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" |
| 30 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
| 31 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 32 | #include "llvm/DebugInfo/PDB/Raw/StreamReader.h" |
| 33 | |
| 34 | #include "llvm/ADT/BitVector.h" |
| 35 | #include "llvm/Support/Endian.h" |
| 36 | #include "llvm/Support/Format.h" |
| 37 | #include "llvm/Support/MathExtras.h" |
| 38 | |
| 39 | using namespace llvm; |
| 40 | using namespace llvm::support; |
| 41 | using namespace llvm::pdb; |
| 42 | |
| 43 | |
| 44 | static const unsigned IPHR_HASH = 4096; |
| 45 | |
| 46 | // This is PSGSIHDR struct defined in |
| 47 | // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h |
| 48 | struct PublicsStream::HeaderInfo { |
| 49 | ulittle32_t SymHash; |
| 50 | ulittle32_t AddrMap; |
| 51 | ulittle32_t NumThunks; |
| 52 | ulittle32_t SizeOfThunk; |
| 53 | ulittle16_t ISectThunkTable; |
| 54 | char Padding[2]; |
| 55 | ulittle32_t OffThunkTable; |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 56 | ulittle32_t NumSections; |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
| 60 | // This is GSIHashHdr struct defined in |
| 61 | struct PublicsStream::GSIHashHeader { |
Reid Kleckner | e1587bc | 2016-05-19 20:20:22 +0000 | [diff] [blame^] | 62 | enum : unsigned { |
| 63 | HdrSignature = ~0U, |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 64 | HdrVersion = 0xeffe0000 + 19990810, |
| 65 | }; |
| 66 | ulittle32_t VerSignature; |
| 67 | ulittle32_t VerHdr; |
| 68 | ulittle32_t HrSize; |
| 69 | ulittle32_t NumBuckets; |
| 70 | }; |
| 71 | |
| 72 | struct PublicsStream::HRFile { |
| 73 | ulittle32_t Off; |
| 74 | ulittle32_t CRef; |
| 75 | }; |
| 76 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 77 | // This struct is defined as "SO" in langapi/include/pdb.h. |
| 78 | namespace { |
| 79 | struct SectionOffset { |
| 80 | ulittle32_t Off; |
| 81 | ulittle16_t Isect; |
| 82 | char Padding[2]; |
| 83 | }; |
| 84 | } |
| 85 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 86 | PublicsStream::PublicsStream(PDBFile &File, uint32_t StreamNum) |
| 87 | : StreamNum(StreamNum), Stream(StreamNum, File) {} |
| 88 | |
| 89 | PublicsStream::~PublicsStream() {} |
| 90 | |
| 91 | uint32_t PublicsStream::getSymHash() const { return Header->SymHash; } |
| 92 | uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; } |
| 93 | |
| 94 | // Publics stream contains fixed-size headers and a serialized hash table. |
| 95 | // This implementation is not complete yet. It reads till the end of the |
| 96 | // stream so that we verify the stream is at least not corrupted. However, |
| 97 | // we skip over the hash table which we believe contains information about |
| 98 | // public symbols. |
| 99 | Error PublicsStream::reload() { |
| 100 | StreamReader Reader(Stream); |
| 101 | |
| 102 | // Check stream size. |
| 103 | if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader)) |
| 104 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 105 | "Publics Stream does not contain a header."); |
| 106 | |
| 107 | // Read PSGSIHDR and GSIHashHdr structs. |
| 108 | Header.reset(new HeaderInfo()); |
| 109 | if (Reader.readObject(Header.get())) |
| 110 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 111 | "Publics Stream does not contain a header."); |
| 112 | HashHdr.reset(new GSIHashHeader()); |
| 113 | if (Reader.readObject(HashHdr.get())) |
| 114 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 115 | "Publics Stream does not contain a header."); |
| 116 | |
| 117 | // An array of HRFile follows. Read them. |
| 118 | if (HashHdr->HrSize % sizeof(HRFile)) |
| 119 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 120 | "Invalid HR array size."); |
| 121 | std::vector<HRFile> HRs(HashHdr->HrSize / sizeof(HRFile)); |
| 122 | if (auto EC = Reader.readArray<HRFile>(HRs)) |
| 123 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 124 | "Could not read an HR array"); |
| 125 | |
| 126 | // A bitmap of a fixed length follows. |
| 127 | size_t BitmapSizeInBits = alignTo(IPHR_HASH + 1, 32); |
| 128 | std::vector<uint8_t> Bitmap(BitmapSizeInBits / 8); |
| 129 | if (auto EC = Reader.readArray<uint8_t>(Bitmap)) |
| 130 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 131 | "Could not read a bitmap."); |
| 132 | for (uint8_t B : Bitmap) |
| 133 | NumBuckets += countPopulation(B); |
| 134 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 135 | // We don't yet understand the following data structures completely, |
| 136 | // but we at least know the types and sizes. Here we are trying |
| 137 | // to read the stream till end so that we at least can detect |
| 138 | // corrupted streams. |
| 139 | |
| 140 | // Hash buckets follow. |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 141 | std::vector<ulittle32_t> TempHashBuckets(NumBuckets); |
Daniel Sanders | c819d90 | 2016-05-18 09:59:14 +0000 | [diff] [blame] | 142 | if (auto EC = Reader.readArray<ulittle32_t>(TempHashBuckets)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 143 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 144 | "Hash buckets corrupted."); |
Daniel Sanders | c819d90 | 2016-05-18 09:59:14 +0000 | [diff] [blame] | 145 | HashBuckets.resize(NumBuckets); |
| 146 | std::copy(TempHashBuckets.begin(), TempHashBuckets.end(), |
| 147 | HashBuckets.begin()); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 148 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 149 | // Something called "address map" follows. |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 150 | std::vector<ulittle32_t> TempAddressMap(Header->AddrMap / sizeof(uint32_t)); |
| 151 | if (auto EC = Reader.readArray<ulittle32_t>(TempAddressMap)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 152 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 153 | "Could not read an address map."); |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 154 | AddressMap.resize(Header->AddrMap / sizeof(uint32_t)); |
| 155 | std::copy(TempAddressMap.begin(), TempAddressMap.end(), AddressMap.begin()); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 156 | |
| 157 | // Something called "thunk map" follows. |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 158 | std::vector<ulittle32_t> TempThunkMap(Header->NumThunks); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 159 | ThunkMap.resize(Header->NumThunks); |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 160 | if (auto EC = Reader.readArray<ulittle32_t>(TempThunkMap)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 161 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 162 | "Could not read a thunk map."); |
Daniel Sanders | 016e6c4 | 2016-05-18 12:36:25 +0000 | [diff] [blame] | 163 | ThunkMap.resize(Header->NumThunks); |
| 164 | std::copy(TempThunkMap.begin(), TempThunkMap.end(), ThunkMap.begin()); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 165 | |
| 166 | // Something called "section map" follows. |
Rui Ueyama | 350b298 | 2016-05-18 16:24:16 +0000 | [diff] [blame] | 167 | std::vector<SectionOffset> Offsets(Header->NumSections); |
| 168 | if (auto EC = Reader.readArray<SectionOffset>(Offsets)) |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 169 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 170 | "Could not read a section map."); |
Rui Ueyama | 350b298 | 2016-05-18 16:24:16 +0000 | [diff] [blame] | 171 | for (auto &SO : Offsets) { |
| 172 | SectionOffsets.push_back(SO.Off); |
| 173 | SectionOffsets.push_back(SO.Isect); |
| 174 | } |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 175 | |
| 176 | if (Reader.bytesRemaining() > 0) |
| 177 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 178 | "Corrupted publics stream."); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 179 | return Error::success(); |
| 180 | } |