blob: a3a44ceddca9fba29ab4cfd7fe1729cd6b32a362 [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"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include "llvm/ADT/iterator_range.h"
27#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000028#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000029#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
30#include "llvm/DebugInfo/PDB/Native/RawError.h"
31#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000032#include "llvm/Support/BinaryStreamReader.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000033#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000034#include "llvm/Support/Error.h"
35#include <algorithm>
36#include <cstdint>
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000037
38using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000039using namespace llvm::msf;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000040using namespace llvm::support;
41using namespace llvm::pdb;
42
Reid Kleckner14d90fd2017-07-26 00:40:36 +000043PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
44 : Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000045
Eugene Zelenko570e39a2016-11-23 23:16:32 +000046PublicsStream::~PublicsStream() = default;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000047
48uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
Zachary Turner5448dab2017-08-09 04:23:59 +000049uint16_t PublicsStream::getThunkTableSection() const {
50 return Header->ISectThunkTable;
51}
52uint32_t PublicsStream::getThunkTableOffset() const {
53 return Header->OffThunkTable;
54}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000055
56// Publics stream contains fixed-size headers and a serialized hash table.
57// This implementation is not complete yet. It reads till the end of the
58// stream so that we verify the stream is at least not corrupted. However,
59// we skip over the hash table which we believe contains information about
60// public symbols.
61Error PublicsStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000062 BinaryStreamReader Reader(*Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000063
64 // Check stream size.
Zachary Turner7eaf1d92017-07-10 22:40:20 +000065 if (Reader.bytesRemaining() <
66 sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000067 return make_error<RawError>(raw_error_code::corrupt_file,
68 "Publics Stream does not contain a header.");
69
Reid Kleckner14d90fd2017-07-26 00:40:36 +000070 // Read PSGSIHDR struct.
Zachary Turner8dbe3622016-05-27 01:54:44 +000071 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000072 return make_error<RawError>(raw_error_code::corrupt_file,
73 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000074
Reid Kleckner14d90fd2017-07-26 00:40:36 +000075 // Read the hash table.
76 if (auto E = PublicsTable.read(Reader))
77 return E;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000078
Rui Ueyama8dc18c52016-05-17 23:07:48 +000079 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000080 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
81 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +000082 return joinErrors(std::move(EC),
83 make_error<RawError>(raw_error_code::corrupt_file,
84 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000085
86 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000087 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +000088 return joinErrors(std::move(EC),
89 make_error<RawError>(raw_error_code::corrupt_file,
90 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000091
92 // Something called "section map" follows.
Zachary Turner349c18f2017-06-05 21:40:33 +000093 if (Reader.bytesRemaining() > 0) {
94 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
95 return joinErrors(std::move(EC),
96 make_error<RawError>(raw_error_code::corrupt_file,
97 "Could not read a section map."));
98 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +000099
100 if (Reader.bytesRemaining() > 0)
101 return make_error<RawError>(raw_error_code::corrupt_file,
102 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000103 return Error::success();
104}