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" |
Zachary Turner | d5d37dc | 2016-05-25 20:37:03 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/CodeView/StreamReader.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
| 30 | #include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h" |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
| 33 | #include "llvm/DebugInfo/PDB/Raw/RawError.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 | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 73 | PublicsStream::PublicsStream(PDBFile &File, uint32_t StreamNum) |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 74 | : Pdb(File), StreamNum(StreamNum), Stream(StreamNum, File) {} |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 75 | |
| 76 | PublicsStream::~PublicsStream() {} |
| 77 | |
| 78 | uint32_t PublicsStream::getSymHash() const { return Header->SymHash; } |
| 79 | uint32_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. |
| 86 | Error PublicsStream::reload() { |
Zachary Turner | d5d37dc | 2016-05-25 20:37:03 +0000 | [diff] [blame] | 87 | codeview::StreamReader Reader(Stream); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 88 | |
| 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 Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 95 | if (Reader.readObject(Header)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 96 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 97 | "Publics Stream does not contain a header."); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 98 | |
| 99 | if (Reader.readObject(HashHdr)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 100 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 101 | "Publics Stream does not contain a header."); |
| 102 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 103 | // An array of HashRecord follows. Read them. |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 104 | if (HashHdr->HrSize % sizeof(PSHashRecord)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 105 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 106 | "Invalid HR array size."); |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 107 | uint32_t NumHashRecords = HashHdr->HrSize / sizeof(PSHashRecord); |
| 108 | if (auto EC = Reader.readArray(HashRecords, NumHashRecords)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 109 | 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 Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 114 | uint32_t NumBitmapEntries = BitmapSizeInBits / 8; |
| 115 | if (auto EC = Reader.readBytes(NumBitmapEntries, Bitmap)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 116 | 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 Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 121 | // 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 Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 127 | if (auto EC = Reader.readArray(HashBuckets, NumBuckets)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 128 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 129 | "Hash buckets corrupted."); |
| 130 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 131 | // Something called "address map" follows. |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 132 | uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t); |
| 133 | if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 134 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 135 | "Could not read an address map."); |
| 136 | |
| 137 | // Something called "thunk map" follows. |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 138 | if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 139 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 140 | "Could not read a thunk map."); |
| 141 | |
| 142 | // Something called "section map" follows. |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame^] | 143 | if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 144 | 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 Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 150 | return Error::success(); |
| 151 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 152 | |
Zachary Turner | 9e33e6f | 2016-05-24 18:55:14 +0000 | [diff] [blame] | 153 | iterator_range<codeview::SymbolIterator> PublicsStream::getSymbols() const { |
| 154 | using codeview::SymbolIterator; |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 155 | auto SymbolS = Pdb.getPDBSymbolStream(); |
Zachary Turner | 9e33e6f | 2016-05-24 18:55:14 +0000 | [diff] [blame] | 156 | if (SymbolS.takeError()) { |
| 157 | return llvm::make_range<SymbolIterator>(SymbolIterator(), SymbolIterator()); |
| 158 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 159 | SymbolStream &SS = SymbolS.get(); |
| 160 | |
Zachary Turner | 9e33e6f | 2016-05-24 18:55:14 +0000 | [diff] [blame] | 161 | return SS.getSymbols(); |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 162 | } |