blob: 9c3e654f808ba283d0b468020a3f6dce9f8378c7 [file] [log] [blame]
Rui Ueyama1f6b6e22016-05-13 21:21:53 +00001//===- 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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000025#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
Bob Haarman653baa22016-10-21 19:43:19 +000026#include "GSI.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027#include "llvm/ADT/iterator_range.h"
28#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000029#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000030#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
31#include "llvm/DebugInfo/PDB/Native/RawError.h"
32#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000033#include "llvm/Support/BinaryStreamReader.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000034#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000035#include "llvm/Support/Error.h"
36#include <algorithm>
37#include <cstdint>
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000038
39using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000040using namespace llvm::msf;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000041using namespace llvm::support;
42using namespace llvm::pdb;
43
Zachary Turnera1657a92016-06-08 17:26:39 +000044PublicsStream::PublicsStream(PDBFile &File,
45 std::unique_ptr<MappedBlockStream> Stream)
46 : Pdb(File), Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000047
Eugene Zelenko570e39a2016-11-23 23:16:32 +000048PublicsStream::~PublicsStream() = default;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000049
50uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
51uint32_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.
58Error PublicsStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000059 BinaryStreamReader Reader(*Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000060
61 // Check stream size.
Zachary Turner7eaf1d92017-07-10 22:40:20 +000062 if (Reader.bytesRemaining() <
63 sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000064 return make_error<RawError>(raw_error_code::corrupt_file,
65 "Publics Stream does not contain a header.");
66
67 // Read PSGSIHDR and GSIHashHdr structs.
Zachary Turner8dbe3622016-05-27 01:54:44 +000068 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000069 return make_error<RawError>(raw_error_code::corrupt_file,
70 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000071
Bob Haarman653baa22016-10-21 19:43:19 +000072 if (auto EC = readGSIHashHeader(HashHdr, Reader))
73 return EC;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000074
Bob Haarman653baa22016-10-21 19:43:19 +000075 if (auto EC = readGSIHashRecords(HashRecords, HashHdr, Reader))
76 return EC;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000077
Bob Haarman653baa22016-10-21 19:43:19 +000078 if (auto EC = readGSIHashBuckets(HashBuckets, HashHdr, Reader))
79 return EC;
80 NumBuckets = HashBuckets.size();
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000081
Rui Ueyama8dc18c52016-05-17 23:07:48 +000082 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000083 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
84 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +000085 return joinErrors(std::move(EC),
86 make_error<RawError>(raw_error_code::corrupt_file,
87 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000088
89 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000090 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +000091 return joinErrors(std::move(EC),
92 make_error<RawError>(raw_error_code::corrupt_file,
93 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000094
95 // Something called "section map" follows.
Zachary Turner349c18f2017-06-05 21:40:33 +000096 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 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000102
103 if (Reader.bytesRemaining() > 0)
104 return make_error<RawError>(raw_error_code::corrupt_file,
105 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000106 return Error::success();
107}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000108
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000109iterator_range<codeview::CVSymbolArray::Iterator>
110PublicsStream::getSymbols(bool *HadError) const {
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000111 auto SymbolS = Pdb.getPDBSymbolStream();
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000112 if (SymbolS.takeError()) {
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000113 codeview::CVSymbolArray::Iterator Iter;
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000114 return make_range(Iter, Iter);
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000115 }
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000116 SymbolStream &SS = SymbolS.get();
117
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000118 return SS.getSymbols(HadError);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000119}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000120
Zachary Turner63055452017-06-15 22:24:24 +0000121Expected<const codeview::CVSymbolArray &>
122PublicsStream::getSymbolArray() const {
123 auto SymbolS = Pdb.getPDBSymbolStream();
124 if (!SymbolS)
125 return SymbolS.takeError();
126
127 return SymbolS->getSymbolArray();
128}
129
Zachary Turner8848a7a2016-07-06 18:05:57 +0000130Error PublicsStream::commit() { return Error::success(); }