LLVM  14.0.0git
PublicsStream.cpp
Go to the documentation of this file.
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 
30 #include "llvm/Support/Endian.h"
31 #include "llvm/Support/Error.h"
32 #include <algorithm>
33 #include <cstdint>
34 
35 using namespace llvm;
36 using namespace llvm::msf;
37 using namespace llvm::support;
38 using namespace llvm::pdb;
39 
40 PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
41  : Stream(std::move(Stream)) {}
42 
44 
45 uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
47  return Header->ISectThunkTable;
48 }
50  return Header->OffThunkTable;
51 }
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 struct.
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  // Read the hash table.
73  if (auto E = PublicsTable.read(Reader))
74  return E;
75 
76  // Something called "address map" follows.
77  uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
78  if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
79  return joinErrors(std::move(EC),
80  make_error<RawError>(raw_error_code::corrupt_file,
81  "Could not read an address map."));
82 
83  // Something called "thunk map" follows.
84  if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
85  return joinErrors(std::move(EC),
86  make_error<RawError>(raw_error_code::corrupt_file,
87  "Could not read a thunk map."));
88 
89  // Something called "section map" follows.
90  if (Reader.bytesRemaining() > 0) {
91  if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
92  return joinErrors(std::move(EC),
93  make_error<RawError>(raw_error_code::corrupt_file,
94  "Could not read a section map."));
95  }
96 
97  if (Reader.bytesRemaining() > 0)
98  return make_error<RawError>(raw_error_code::corrupt_file,
99  "Corrupted publics stream.");
100  return Error::success();
101 }
BinaryStreamReader.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
PublicsStream.h
llvm::pdb::PublicsStreamHeader::AddrMap
support::ulittle32_t AddrMap
Definition: RawTypes.h:266
llvm::pdb::PublicsStreamHeader::ISectThunkTable
support::ulittle16_t ISectThunkTable
Definition: RawTypes.h:269
SymbolRecord.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::pdb::PublicsStream::reload
Error reload()
Definition: PublicsStream.cpp:58
Error.h
llvm::BinaryStreamReader::bytesRemaining
uint64_t bytesRemaining() const
Definition: BinaryStreamReader.h:257
RawError.h
llvm::msf
Definition: IMSFFile.h:18
llvm::pdb::GSIHashHeader
Header of the hash tables found in the globals and publics sections.
Definition: RawTypes.h:28
llvm::pdb::PublicsStream::getThunkTableSection
uint16_t getThunkTableSection() const
Definition: PublicsStream.cpp:46
llvm::pdb::raw_error_code::corrupt_file
@ corrupt_file
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::pdb
Definition: ConcreteSymbolEnumerator.h:20
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
llvm::pdb::PublicsStreamHeader::OffThunkTable
support::ulittle32_t OffThunkTable
Definition: RawTypes.h:271
llvm::pdb::PublicsStreamHeader::SymHash
support::ulittle32_t SymHash
Definition: RawTypes.h:265
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:428
MappedBlockStream.h
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1607
iterator_range.h
llvm::BinaryStreamReader::readObject
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...
Definition: BinaryStreamReader.h:169
uint32_t
std
Definition: BitVector.h:838
uint16_t
llvm::pdb::PublicsStream::getThunkTableOffset
uint32_t getThunkTableOffset() const
Definition: PublicsStream.cpp:49
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::support
Definition: Endian.h:25
llvm::pdb::PublicsStream::~PublicsStream
~PublicsStream()
llvm::pdb::PublicsStreamHeader::NumSections
support::ulittle32_t NumSections
Definition: RawTypes.h:272
llvm::pdb::PublicsStreamHeader::NumThunks
support::ulittle32_t NumThunks
Definition: RawTypes.h:267
llvm::pdb::PublicsStreamHeader
Definition: RawTypes.h:264
Endian.h
llvm::BinaryStreamReader::readArray
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 ...
Definition: BinaryStreamReader.h:187
llvm::pdb::GSIHashTable::read
Error read(BinaryStreamReader &Reader)
Definition: GlobalsStream.cpp:171
llvm::pdb::PublicsStream::getSymHash
uint32_t getSymHash() const
Definition: PublicsStream.cpp:45