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/Native/PublicsStream.h" 26 #include "GSI.h" 27 #include "llvm/ADT/iterator_range.h" 28 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 29 #include "llvm/DebugInfo/MSF/MappedBlockStream.h" 30 #include "llvm/DebugInfo/PDB/Native/PDBFile.h" 31 #include "llvm/DebugInfo/PDB/Native/RawError.h" 32 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" 33 #include "llvm/Support/BinaryStreamReader.h" 34 #include "llvm/Support/Endian.h" 35 #include "llvm/Support/Error.h" 36 #include <algorithm> 37 #include <cstdint> 38 39 using namespace llvm; 40 using namespace llvm::msf; 41 using namespace llvm::support; 42 using namespace llvm::pdb; 43 44 // This is PSGSIHDR struct defined in 45 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h 46 struct PublicsStream::HeaderInfo { 47 ulittle32_t SymHash; 48 ulittle32_t AddrMap; 49 ulittle32_t NumThunks; 50 ulittle32_t SizeOfThunk; 51 ulittle16_t ISectThunkTable; 52 char Padding[2]; 53 ulittle32_t OffThunkTable; 54 ulittle32_t NumSections; 55 }; 56 57 PublicsStream::PublicsStream(PDBFile &File, 58 std::unique_ptr<MappedBlockStream> Stream) 59 : Pdb(File), Stream(std::move(Stream)) {} 60 61 PublicsStream::~PublicsStream() = default; 62 63 uint32_t PublicsStream::getSymHash() const { return Header->SymHash; } 64 uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; } 65 66 // Publics stream contains fixed-size headers and a serialized hash table. 67 // This implementation is not complete yet. It reads till the end of the 68 // stream so that we verify the stream is at least not corrupted. However, 69 // we skip over the hash table which we believe contains information about 70 // public symbols. 71 Error PublicsStream::reload() { 72 BinaryStreamReader Reader(*Stream); 73 74 // Check stream size. 75 if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader)) 76 return make_error<RawError>(raw_error_code::corrupt_file, 77 "Publics Stream does not contain a header."); 78 79 // Read PSGSIHDR and GSIHashHdr structs. 80 if (Reader.readObject(Header)) 81 return make_error<RawError>(raw_error_code::corrupt_file, 82 "Publics Stream does not contain a header."); 83 84 if (auto EC = readGSIHashHeader(HashHdr, Reader)) 85 return EC; 86 87 if (auto EC = readGSIHashRecords(HashRecords, HashHdr, Reader)) 88 return EC; 89 90 if (auto EC = readGSIHashBuckets(HashBuckets, HashHdr, Reader)) 91 return EC; 92 NumBuckets = HashBuckets.size(); 93 94 // Something called "address map" follows. 95 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t); 96 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries)) 97 return joinErrors(std::move(EC), 98 make_error<RawError>(raw_error_code::corrupt_file, 99 "Could not read an address map.")); 100 101 // Something called "thunk map" follows. 102 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks)) 103 return joinErrors(std::move(EC), 104 make_error<RawError>(raw_error_code::corrupt_file, 105 "Could not read a thunk map.")); 106 107 // Something called "section map" follows. 108 if (Reader.bytesRemaining() > 0) { 109 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections)) 110 return joinErrors(std::move(EC), 111 make_error<RawError>(raw_error_code::corrupt_file, 112 "Could not read a section map.")); 113 } 114 115 if (Reader.bytesRemaining() > 0) 116 return make_error<RawError>(raw_error_code::corrupt_file, 117 "Corrupted publics stream."); 118 return Error::success(); 119 } 120 121 iterator_range<codeview::CVSymbolArray::Iterator> 122 PublicsStream::getSymbols(bool *HadError) const { 123 auto SymbolS = Pdb.getPDBSymbolStream(); 124 if (SymbolS.takeError()) { 125 codeview::CVSymbolArray::Iterator Iter; 126 return make_range(Iter, Iter); 127 } 128 SymbolStream &SS = SymbolS.get(); 129 130 return SS.getSymbols(HadError); 131 } 132 133 Expected<const codeview::CVSymbolArray &> 134 PublicsStream::getSymbolArray() const { 135 auto SymbolS = Pdb.getPDBSymbolStream(); 136 if (!SymbolS) 137 return SymbolS.takeError(); 138 139 return SymbolS->getSymbolArray(); 140 } 141 142 Error PublicsStream::commit() { return Error::success(); } 143