blob: a33bf03bf8fb312e116a5864b81f9b09fc80790b [file] [log] [blame]
Rui Ueyama1f6b6e22016-05-13 21:21:53 +00001//===- PublicsStream.cpp - PDB Public Symbol Stream -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rui Ueyama1f6b6e22016-05-13 21:21:53 +00006//
7//===----------------------------------------------------------------------===//
8//
9// The data structures defined in this file are based on the reference
10// implementation which is available at
11// https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
12//
13// When you are reading the reference source code, you'd find the
14// information below useful.
15//
16// - ppdb1->m_fMinimalDbgInfo seems to be always true.
17// - SMALLBUCKETS macro is defined.
18//
19// The reference doesn't compile, so I learned just by reading code.
20// It's not guaranteed to be correct.
21//
22//===----------------------------------------------------------------------===//
23
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000024#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000025#include "llvm/ADT/iterator_range.h"
26#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000027#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000028#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000029#include "llvm/Support/BinaryStreamReader.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000030#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000031#include "llvm/Support/Error.h"
32#include <algorithm>
33#include <cstdint>
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000034
35using namespace llvm;
Zachary Turnerbac69d32016-07-22 19:56:05 +000036using namespace llvm::msf;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000037using namespace llvm::support;
38using namespace llvm::pdb;
39
Reid Kleckner14d90fd2017-07-26 00:40:36 +000040PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
41 : Stream(std::move(Stream)) {}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000042
Eugene Zelenko570e39a2016-11-23 23:16:32 +000043PublicsStream::~PublicsStream() = default;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000044
45uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
Zachary Turner5448dab2017-08-09 04:23:59 +000046uint16_t PublicsStream::getThunkTableSection() const {
47 return Header->ISectThunkTable;
48}
49uint32_t PublicsStream::getThunkTableOffset() const {
50 return Header->OffThunkTable;
51}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000052
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
Reid Kleckner14d90fd2017-07-26 00:40:36 +000067 // Read PSGSIHDR struct.
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
Reid Kleckner14d90fd2017-07-26 00:40:36 +000072 // Read the hash table.
73 if (auto E = PublicsTable.read(Reader))
74 return E;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000075
Rui Ueyama8dc18c52016-05-17 23:07:48 +000076 // Something called "address map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000077 uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
78 if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
David Majnemer836937e2016-05-27 16:16:56 +000079 return joinErrors(std::move(EC),
80 make_error<RawError>(raw_error_code::corrupt_file,
81 "Could not read an address map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000082
83 // Something called "thunk map" follows.
Zachary Turnerb393d952016-05-27 03:51:53 +000084 if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
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 a thunk map."));
Rui Ueyama8dc18c52016-05-17 23:07:48 +000088
89 // Something called "section map" follows.
Zachary Turner349c18f2017-06-05 21:40:33 +000090 if (Reader.bytesRemaining() > 0) {
91 if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
92 return joinErrors(std::move(EC),
93 make_error<RawError>(raw_error_code::corrupt_file,
94 "Could not read a section map."));
95 }
Rui Ueyama8dc18c52016-05-17 23:07:48 +000096
97 if (Reader.bytesRemaining() > 0)
98 return make_error<RawError>(raw_error_code::corrupt_file,
99 "Corrupted publics stream.");
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000100 return Error::success();
101}