LLVM  5.0.0
PublicsStream.cpp
Go to the documentation of this file.
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 
26 #include "GSI.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 
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.
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, 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 
110 PublicsStream::getSymbols(bool *HadError) const {
111  auto SymbolS = Pdb.getPDBSymbolStream();
112  if (SymbolS.takeError()) {
114  return make_range(Iter, Iter);
115  }
116  SymbolStream &SS = SymbolS.get();
117 
118  return SS.getSymbols(HadError);
119 }
120 
123  auto SymbolS = Pdb.getPDBSymbolStream();
124  if (!SymbolS)
125  return SymbolS.takeError();
126 
127  return SymbolS->getSymbolArray();
128 }
129 
iterator_range< codeview::CVSymbolArray::Iterator > getSymbols(bool *HadError) const
Expected< const codeview::CVSymbolArray & > getSymbolArray() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
support::ulittle32_t NumThunks
Definition: RawTypes.h:264
Header of the hash tables found in the globals and publics sections.
Definition: GSI.h:46
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Error readGSIHashRecords(FixedStreamArray< PSHashRecord > &HashRecords, const GSIHashHeader *HashHdr, BinaryStreamReader &Reader)
Definition: GSI.cpp:73
support::ulittle32_t SymHash
Definition: RawTypes.h:262
uint32_t bytesRemaining() const
Expected< SymbolStream & > getPDBSymbolStream()
Definition: PDBFile.cpp:330
Error readGSIHashBuckets(FixedStreamArray< support::ulittle32_t > &HashBuckets, const GSIHashHeader *HashHdr, BinaryStreamReader &Reader)
Definition: GSI.cpp:31
uint32_t getSymHash() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
support::ulittle32_t AddrMap
Definition: RawTypes.h:263
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
VarStreamArray represents an array of variable length records backed by a stream. ...
A range adaptor for a pair of iterators.
iterator_range< codeview::CVSymbolArray::Iterator > getSymbols(bool *HadError) const
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:406
uint32_t getAddrMap() const
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Provides read only access to a subclass of BinaryStream.
support::ulittle32_t NumSections
Definition: RawTypes.h:269
Error readGSIHashHeader(const GSIHashHeader *&HashHdr, BinaryStreamReader &Reader)
Definition: GSI.cpp:59
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
uint32_t size() const