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 PublicsStream::PublicsStream(PDBFile &File, 45 std::unique_ptr<MappedBlockStream> Stream) 46 : Pdb(File), Stream(std::move(Stream)) {} 47 48 PublicsStream::~PublicsStream() = default; 49 50 uint32_t PublicsStream::getSymHash() const { return Header->SymHash; } 51 uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; } 52 53 // Publics stream contains fixed-size headers and a serialized hash table. 54 // This implementation is not complete yet. It reads till the end of the 55 // stream so that we verify the stream is at least not corrupted. However, 56 // we skip over the hash table which we believe contains information about 57 // public symbols. 58 Error PublicsStream::reload() { 59 BinaryStreamReader Reader(*Stream); 60 61 // Check stream size. 62 if (Reader.bytesRemaining() < 63 sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader)) 64 return make_error<RawError>(raw_error_code::corrupt_file, 65 "Publics Stream does not contain a header."); 66 67 // Read PSGSIHDR and GSIHashHdr structs. 68 if (Reader.readObject(Header)) 69 return make_error<RawError>(raw_error_code::corrupt_file, 70 "Publics Stream does not contain a header."); 71 72 if (auto EC = readGSIHashHeader(HashHdr, Reader)) 73 return EC; 74 75 if (auto EC = readGSIHashRecords(HashRecords, HashHdr, Reader)) 76 return EC; 77 78 if (auto EC = readGSIHashBuckets(HashBuckets, HashBitmap, HashHdr, Reader)) 79 return EC; 80 NumBuckets = HashBuckets.size(); 81 82 // Something called "address map" follows. 83 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t); 84 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries)) 85 return joinErrors(std::move(EC), 86 make_error<RawError>(raw_error_code::corrupt_file, 87 "Could not read an address map.")); 88 89 // Something called "thunk map" follows. 90 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks)) 91 return joinErrors(std::move(EC), 92 make_error<RawError>(raw_error_code::corrupt_file, 93 "Could not read a thunk map.")); 94 95 // Something called "section map" follows. 96 if (Reader.bytesRemaining() > 0) { 97 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections)) 98 return joinErrors(std::move(EC), 99 make_error<RawError>(raw_error_code::corrupt_file, 100 "Could not read a section map.")); 101 } 102 103 if (Reader.bytesRemaining() > 0) 104 return make_error<RawError>(raw_error_code::corrupt_file, 105 "Corrupted publics stream."); 106 return Error::success(); 107 } 108 109 iterator_range<codeview::CVSymbolArray::Iterator> 110 PublicsStream::getSymbols(bool *HadError) const { 111 auto SymbolS = Pdb.getPDBSymbolStream(); 112 if (SymbolS.takeError()) { 113 codeview::CVSymbolArray::Iterator Iter; 114 return make_range(Iter, Iter); 115 } 116 SymbolStream &SS = SymbolS.get(); 117 118 return SS.getSymbols(HadError); 119 } 120 121 Expected<const codeview::CVSymbolArray &> 122 PublicsStream::getSymbolArray() const { 123 auto SymbolS = Pdb.getPDBSymbolStream(); 124 if (!SymbolS) 125 return SymbolS.takeError(); 126 127 return SymbolS->getSymbolArray(); 128 } 129 130 Error PublicsStream::commit() { return Error::success(); } 131