xref: /llvm-project/llvm/lib/DebugInfo/PDB/Native/PublicsStream.cpp (revision 14d90fd05cbed5fd3fcee492f072a4e5816f20b5)
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(std::unique_ptr<MappedBlockStream> Stream)
45     : Stream(std::move(Stream)) {}
46 
47 PublicsStream::~PublicsStream() = default;
48 
49 uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
50 uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
51 
52 // Publics stream contains fixed-size headers and a serialized hash table.
53 // This implementation is not complete yet. It reads till the end of the
54 // stream so that we verify the stream is at least not corrupted. However,
55 // we skip over the hash table which we believe contains information about
56 // public symbols.
57 Error PublicsStream::reload() {
58   BinaryStreamReader Reader(*Stream);
59 
60   // Check stream size.
61   if (Reader.bytesRemaining() <
62       sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
63     return make_error<RawError>(raw_error_code::corrupt_file,
64                                 "Publics Stream does not contain a header.");
65 
66   // Read PSGSIHDR struct.
67   if (Reader.readObject(Header))
68     return make_error<RawError>(raw_error_code::corrupt_file,
69                                 "Publics Stream does not contain a header.");
70 
71   // Read the hash table.
72   if (auto E = PublicsTable.read(Reader))
73     return E;
74 
75   // Something called "address map" follows.
76   uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
77   if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
78     return joinErrors(std::move(EC),
79                       make_error<RawError>(raw_error_code::corrupt_file,
80                                            "Could not read an address map."));
81 
82   // Something called "thunk map" follows.
83   if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
84     return joinErrors(std::move(EC),
85                       make_error<RawError>(raw_error_code::corrupt_file,
86                                            "Could not read a thunk map."));
87 
88   // Something called "section map" follows.
89   if (Reader.bytesRemaining() > 0) {
90     if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
91       return joinErrors(std::move(EC),
92                         make_error<RawError>(raw_error_code::corrupt_file,
93                                              "Could not read a section map."));
94   }
95 
96   if (Reader.bytesRemaining() > 0)
97     return make_error<RawError>(raw_error_code::corrupt_file,
98                                 "Corrupted publics stream.");
99   return Error::success();
100 }
101