1 //===- PublicsStream.cpp - PDB Public Symbol Stream -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The data structures defined in this file are based on the reference
10 // implementation which is available at
11 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
12 //
13 // When you are reading the reference source code, you'd find the
14 // information below useful.
15 //
16 // - ppdb1->m_fMinimalDbgInfo seems to be always true.
17 // - SMALLBUCKETS macro is defined.
18 //
19 // The reference doesn't compile, so I learned just by reading code.
20 // It's not guaranteed to be correct.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
25 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
26 #include "llvm/DebugInfo/PDB/Native/RawError.h"
27 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
28 #include "llvm/Support/BinaryStreamReader.h"
29 #include "llvm/Support/Endian.h"
30 #include "llvm/Support/Error.h"
31 #include <cstdint>
32
33 using namespace llvm;
34 using namespace llvm::msf;
35 using namespace llvm::support;
36 using namespace llvm::pdb;
37
PublicsStream(std::unique_ptr<MappedBlockStream> Stream)38 PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
39 : Stream(std::move(Stream)) {}
40
41 PublicsStream::~PublicsStream() = default;
42
getSymHash() const43 uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
getThunkTableSection() const44 uint16_t PublicsStream::getThunkTableSection() const {
45 return Header->ISectThunkTable;
46 }
getThunkTableOffset() const47 uint32_t PublicsStream::getThunkTableOffset() const {
48 return Header->OffThunkTable;
49 }
50
51 // Publics stream contains fixed-size headers and a serialized hash table.
52 // This implementation is not complete yet. It reads till the end of the
53 // stream so that we verify the stream is at least not corrupted. However,
54 // we skip over the hash table which we believe contains information about
55 // public symbols.
reload()56 Error PublicsStream::reload() {
57 BinaryStreamReader Reader(*Stream);
58
59 // Check stream size.
60 if (Reader.bytesRemaining() <
61 sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
62 return make_error<RawError>(raw_error_code::corrupt_file,
63 "Publics Stream does not contain a header.");
64
65 // Read PSGSIHDR struct.
66 if (Reader.readObject(Header))
67 return make_error<RawError>(raw_error_code::corrupt_file,
68 "Publics Stream does not contain a header.");
69
70 // Read the hash table.
71 if (auto E = PublicsTable.read(Reader))
72 return E;
73
74 // Something called "address map" follows.
75 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
76 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
77 return joinErrors(std::move(EC),
78 make_error<RawError>(raw_error_code::corrupt_file,
79 "Could not read an address map."));
80
81 // Something called "thunk map" follows.
82 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
83 return joinErrors(std::move(EC),
84 make_error<RawError>(raw_error_code::corrupt_file,
85 "Could not read a thunk map."));
86
87 // Something called "section map" follows.
88 if (Reader.bytesRemaining() > 0) {
89 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
90 return joinErrors(std::move(EC),
91 make_error<RawError>(raw_error_code::corrupt_file,
92 "Could not read a section map."));
93 }
94
95 if (Reader.bytesRemaining() > 0)
96 return make_error<RawError>(raw_error_code::corrupt_file,
97 "Corrupted publics stream.");
98 return Error::success();
99 }
100