Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 1 | //===- 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 Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 30 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
| 32 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 33 | #include "llvm/DebugInfo/PDB/Raw/StreamReader.h" |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 34 | #include "llvm/DebugInfo/PDB/Raw/SymbolStream.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 35 | |
| 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 | |
| 41 | using namespace llvm; |
| 42 | using namespace llvm::support; |
| 43 | using namespace llvm::pdb; |
| 44 | |
| 45 | |
| 46 | static 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 |
| 50 | struct 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 Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 58 | ulittle32_t NumSections; |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 61 | // This is GSIHashHdr. |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 62 | struct PublicsStream::GSIHashHeader { |
Reid Kleckner | e1587bc | 2016-05-19 20:20:22 +0000 | [diff] [blame] | 63 | enum : unsigned { |
| 64 | HdrSignature = ~0U, |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 65 | HdrVersion = 0xeffe0000 + 19990810, |
| 66 | }; |
| 67 | ulittle32_t VerSignature; |
| 68 | ulittle32_t VerHdr; |
| 69 | ulittle32_t HrSize; |
| 70 | ulittle32_t NumBuckets; |
| 71 | }; |
| 72 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 73 | // This is HRFile. |
| 74 | struct PublicsStream::HashRecord { |
| 75 | ulittle32_t Off; // Offset in the symbol record stream |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 76 | ulittle32_t CRef; |
| 77 | }; |
| 78 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 79 | // This struct is defined as "SO" in langapi/include/pdb.h. |
| 80 | namespace { |
| 81 | struct SectionOffset { |
| 82 | ulittle32_t Off; |
| 83 | ulittle16_t Isect; |
| 84 | char Padding[2]; |
| 85 | }; |
| 86 | } |
| 87 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 88 | PublicsStream::PublicsStream(PDBFile &File, uint32_t StreamNum) |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 89 | : Pdb(File), StreamNum(StreamNum), Stream(StreamNum, File) {} |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 90 | |
| 91 | PublicsStream::~PublicsStream() {} |
| 92 | |
| 93 | uint32_t PublicsStream::getSymHash() const { return Header->SymHash; } |
| 94 | uint32_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. |
| 101 | Error 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 Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 119 | // An array of HashRecord follows. Read them. |
| 120 | if (HashHdr->HrSize % sizeof(HashRecord)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 121 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 122 | "Invalid HR array size."); |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 123 | HashRecords.resize(HashHdr->HrSize / sizeof(HashRecord)); |
| 124 | if (auto EC = Reader.readArray<HashRecord>(HashRecords)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 125 | 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 Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 137 | // 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 Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 143 | std::vector<ulittle32_t> TempHashBuckets(NumBuckets); |
Daniel Sanders | c819d90 | 2016-05-18 09:59:14 +0000 | [diff] [blame] | 144 | if (auto EC = Reader.readArray<ulittle32_t>(TempHashBuckets)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 145 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 146 | "Hash buckets corrupted."); |
Daniel Sanders | c819d90 | 2016-05-18 09:59:14 +0000 | [diff] [blame] | 147 | HashBuckets.resize(NumBuckets); |
| 148 | std::copy(TempHashBuckets.begin(), TempHashBuckets.end(), |
| 149 | HashBuckets.begin()); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 150 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 151 | // Something called "address map" follows. |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 152 | std::vector<ulittle32_t> TempAddressMap(Header->AddrMap / sizeof(uint32_t)); |
| 153 | if (auto EC = Reader.readArray<ulittle32_t>(TempAddressMap)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 154 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 155 | "Could not read an address map."); |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 156 | AddressMap.resize(Header->AddrMap / sizeof(uint32_t)); |
| 157 | std::copy(TempAddressMap.begin(), TempAddressMap.end(), AddressMap.begin()); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 158 | |
| 159 | // Something called "thunk map" follows. |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 160 | std::vector<ulittle32_t> TempThunkMap(Header->NumThunks); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 161 | ThunkMap.resize(Header->NumThunks); |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 162 | if (auto EC = Reader.readArray<ulittle32_t>(TempThunkMap)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 163 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 164 | "Could not read a thunk map."); |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 165 | ThunkMap.resize(Header->NumThunks); |
| 166 | std::copy(TempThunkMap.begin(), TempThunkMap.end(), ThunkMap.begin()); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 167 | |
| 168 | // Something called "section map" follows. |
Rui Ueyama | 350b298 | 2016-05-18 16:24:16 +0000 | [diff] [blame] | 169 | std::vector<SectionOffset> Offsets(Header->NumSections); |
| 170 | if (auto EC = Reader.readArray<SectionOffset>(Offsets)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 171 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 172 | "Could not read a section map."); |
Rui Ueyama | 350b298 | 2016-05-18 16:24:16 +0000 | [diff] [blame] | 173 | for (auto &SO : Offsets) { |
| 174 | SectionOffsets.push_back(SO.Off); |
| 175 | SectionOffsets.push_back(SO.Isect); |
| 176 | } |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 177 | |
| 178 | if (Reader.bytesRemaining() > 0) |
| 179 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 180 | "Corrupted publics stream."); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 181 | return Error::success(); |
| 182 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame^] | 183 | |
| 184 | std::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 | } |