blob: 7a48e3d15960e0e721215594f38593dc5f2cead9 [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
Reid Kleckner14d90fd2017-07-26 00:40:36 +000044PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
45 : Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000046
Eugene Zelenko570e39a2016-11-23 23:16:32 +000047PublicsStream::~PublicsStream() = default;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000048
49uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
50uint32_t PublicsStream::getAddrMap() const { return Header->AddrMap; }
51
52// Publics stream contains fixed-size headers and a serialized hash table.
53// This implementation is not complete yet. It reads till the end of the
54// stream so that we verify the stream is at least not corrupted. However,
55// we skip over the hash table which we believe contains information about
56// public symbols.
57Error PublicsStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000058 BinaryStreamReader Reader(*Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000059
60 // Check stream size.
Zachary Turner7eaf1d92017-07-10 22:40:20 +000061 if (Reader.bytesRemaining() <
62 sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000063 return make_error<RawError>(raw_error_code::corrupt_file,
64 "Publics Stream does not contain a header.");
65
Reid Kleckner14d90fd2017-07-26 00:40:36 +000066 // Read PSGSIHDR struct.
Zachary Turner8dbe3622016-05-27 01:54:44 +000067 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000068 return make_error<RawError>(raw_error_code::corrupt_file,
69 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000070
Reid Kleckner14d90fd2017-07-26 00:40:36 +000071 // Read the hash table.
72 if (auto E = PublicsTable.read(Reader))
73 return E;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000074
Rui Ueyama8dc18c52016-05-17 23:07:48 +000075 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000076 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
77 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +000078 return joinErrors(std::move(EC),
79 make_error<RawError>(raw_error_code::corrupt_file,
80 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000081
82 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000083 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +000084 return joinErrors(std::move(EC),
85 make_error<RawError>(raw_error_code::corrupt_file,
86 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000087
88 // Something called "section map" follows.
Zachary Turner349c18f2017-06-05 21:40:33 +000089 if (Reader.bytesRemaining() > 0) {
90 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
91 return joinErrors(std::move(EC),
92 make_error<RawError>(raw_error_code::corrupt_file,
93 "Could not read a section map."));
94 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +000095
96 if (Reader.bytesRemaining() > 0)
97 return make_error<RawError>(raw_error_code::corrupt_file,
98 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000099 return Error::success();
100}