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