blob: 43ac4e5fbdd83da5a99be70fd8407468559fd44e [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
25#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
26
Bob Haarman653baa22016-10-21 19:43:19 +000027#include "GSI.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000028#include "llvm/DebugInfo/CodeView/CodeView.h"
29#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000030#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
31#include "llvm/DebugInfo/MSF/StreamReader.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000032#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000033#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
34#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000035#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000036
37#include "llvm/ADT/BitVector.h"
38#include "llvm/Support/Endian.h"
39#include "llvm/Support/Format.h"
40#include "llvm/Support/MathExtras.h"
41
42using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000043using namespace llvm::msf;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000044using namespace llvm::support;
45using namespace llvm::pdb;
46
47
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000048// This is PSGSIHDR struct defined in
49// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
50struct PublicsStream::HeaderInfo {
51 ulittle32_t SymHash;
52 ulittle32_t AddrMap;
53 ulittle32_t NumThunks;
54 ulittle32_t SizeOfThunk;
55 ulittle16_t ISectThunkTable;
56 char Padding[2];
57 ulittle32_t OffThunkTable;
Rui Ueyama8dc18c52016-05-17 23:07:48 +000058 ulittle32_t NumSections;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000059};
60
Zachary Turnera1657a92016-06-08 17:26:39 +000061PublicsStream::PublicsStream(PDBFile &File,
62 std::unique_ptr<MappedBlockStream> Stream)
63 : Pdb(File), Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000064
65PublicsStream::~PublicsStream() {}
66
67uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
68uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
69
70// Publics stream contains fixed-size headers and a serialized hash table.
71// This implementation is not complete yet. It reads till the end of the
72// stream so that we verify the stream is at least not corrupted. However,
73// we skip over the hash table which we believe contains information about
74// public symbols.
75Error PublicsStream::reload() {
Zachary Turnerbac69d32016-07-22 19:56:05 +000076 StreamReader Reader(*Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000077
78 // Check stream size.
79 if (Reader.bytesRemaining() < sizeof(HeaderInfo) + sizeof(GSIHashHeader))
80 return make_error<RawError>(raw_error_code::corrupt_file,
81 "Publics Stream does not contain a header.");
82
83 // Read PSGSIHDR and GSIHashHdr structs.
Zachary Turner8dbe3622016-05-27 01:54:44 +000084 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000085 return make_error<RawError>(raw_error_code::corrupt_file,
86 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000087
Bob Haarman653baa22016-10-21 19:43:19 +000088 if (auto EC = readGSIHashHeader(HashHdr, Reader))
89 return EC;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000090
Bob Haarman653baa22016-10-21 19:43:19 +000091 if (auto EC = readGSIHashRecords(HashRecords, HashHdr, Reader))
92 return EC;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000093
Bob Haarman653baa22016-10-21 19:43:19 +000094 if (auto EC = readGSIHashBuckets(HashBuckets, HashHdr, Reader))
95 return EC;
96 NumBuckets = HashBuckets.size();
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000097
Rui Ueyama8dc18c52016-05-17 23:07:48 +000098 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000099 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
100 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +0000101 return joinErrors(std::move(EC),
102 make_error<RawError>(raw_error_code::corrupt_file,
103 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000104
105 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000106 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +0000107 return joinErrors(std::move(EC),
108 make_error<RawError>(raw_error_code::corrupt_file,
109 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000110
111 // Something called "section map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +0000112 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
David Majnemer836937e2016-05-27 16:16:56 +0000113 return joinErrors(std::move(EC),
114 make_error<RawError>(raw_error_code::corrupt_file,
115 "Could not read a section map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000116
117 if (Reader.bytesRemaining() > 0)
118 return make_error<RawError>(raw_error_code::corrupt_file,
119 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000120 return Error::success();
121}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000122
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000123iterator_range<codeview::CVSymbolArray::Iterator>
124PublicsStream::getSymbols(bool *HadError) const {
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000125 auto SymbolS = Pdb.getPDBSymbolStream();
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000126 if (SymbolS.takeError()) {
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000127 codeview::CVSymbolArray::Iterator Iter;
128 return llvm::make_range(Iter, Iter);
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000129 }
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000130 SymbolStream &SS = SymbolS.get();
131
Zachary Turner0d43c1c2016-05-28 05:21:57 +0000132 return SS.getSymbols(HadError);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000133}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000134
135Error PublicsStream::commit() { return Error::success(); }