blob: f6466eb80464d3f0f8c8f51ebdbd5f4166db72a3 [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/RawError.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000030#include "llvm/Support/BinaryStreamReader.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000031#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000032#include "llvm/Support/Error.h"
33#include <algorithm>
34#include <cstdint>
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000035
36using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000037using namespace llvm::msf;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000038using namespace llvm::support;
39using namespace llvm::pdb;
40
Reid Kleckner14d90fd2017-07-26 00:40:36 +000041PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
42 : Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000043
Eugene Zelenko570e39a2016-11-23 23:16:32 +000044PublicsStream::~PublicsStream() = default;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000045
46uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
Zachary Turner5448dab2017-08-09 04:23:59 +000047uint16_t PublicsStream::getThunkTableSection() const {
48 return Header->ISectThunkTable;
49}
50uint32_t PublicsStream::getThunkTableOffset() const {
51 return Header->OffThunkTable;
52}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000053
54// Publics stream contains fixed-size headers and a serialized hash table.
55// This implementation is not complete yet. It reads till the end of the
56// stream so that we verify the stream is at least not corrupted. However,
57// we skip over the hash table which we believe contains information about
58// public symbols.
59Error PublicsStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000060 BinaryStreamReader Reader(*Stream);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000061
62 // Check stream size.
Zachary Turner7eaf1d92017-07-10 22:40:20 +000063 if (Reader.bytesRemaining() <
64 sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000065 return make_error<RawError>(raw_error_code::corrupt_file,
66 "Publics Stream does not contain a header.");
67
Reid Kleckner14d90fd2017-07-26 00:40:36 +000068 // Read PSGSIHDR struct.
Zachary Turner8dbe3622016-05-27 01:54:44 +000069 if (Reader.readObject(Header))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000070 return make_error<RawError>(raw_error_code::corrupt_file,
71 "Publics Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000072
Reid Kleckner14d90fd2017-07-26 00:40:36 +000073 // Read the hash table.
74 if (auto E = PublicsTable.read(Reader))
75 return E;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000076
Rui Ueyama8dc18c52016-05-17 23:07:48 +000077 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000078 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
79 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +000080 return joinErrors(std::move(EC),
81 make_error<RawError>(raw_error_code::corrupt_file,
82 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000083
84 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000085 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
David Majnemer836937e2016-05-27 16:16:56 +000086 return joinErrors(std::move(EC),
87 make_error<RawError>(raw_error_code::corrupt_file,
88 "Could not read a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000089
90 // Something called "section map" follows.
Zachary Turner349c18f2017-06-05 21:40:33 +000091 if (Reader.bytesRemaining() > 0) {
92 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
93 return joinErrors(std::move(EC),
94 make_error<RawError>(raw_error_code::corrupt_file,
95 "Could not read a section map."));
96 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +000097
98 if (Reader.bytesRemaining() > 0)
99 return make_error<RawError>(raw_error_code::corrupt_file,
100 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000101 return Error::success();
102}