Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 1 | //===- PublicsStream.cpp - PDB Public Symbol Stream -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 6 | // |
| 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 McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/PDB/Native/PublicsStream.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/iterator_range.h" |
| 26 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 29 | #include "llvm/Support/BinaryStreamReader.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Error.h" |
| 32 | #include <algorithm> |
| 33 | #include <cstdint> |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 36 | using namespace llvm::msf; |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 37 | using namespace llvm::support; |
| 38 | using namespace llvm::pdb; |
| 39 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 40 | PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream) |
| 41 | : Stream(std::move(Stream)) {} |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 42 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 43 | PublicsStream::~PublicsStream() = default; |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 44 | |
| 45 | uint32_t PublicsStream::getSymHash() const { return Header->SymHash; } |
Zachary Turner | 5448dab | 2017-08-09 04:23:59 +0000 | [diff] [blame] | 46 | uint16_t PublicsStream::getThunkTableSection() const { |
| 47 | return Header->ISectThunkTable; |
| 48 | } |
| 49 | uint32_t PublicsStream::getThunkTableOffset() const { |
| 50 | return Header->OffThunkTable; |
| 51 | } |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 52 | |
| 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. |
| 58 | Error PublicsStream::reload() { |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 59 | BinaryStreamReader Reader(*Stream); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 60 | |
| 61 | // Check stream size. |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 62 | if (Reader.bytesRemaining() < |
| 63 | sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 64 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 65 | "Publics Stream does not contain a header."); |
| 66 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 67 | // Read PSGSIHDR struct. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 68 | if (Reader.readObject(Header)) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 69 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 70 | "Publics Stream does not contain a header."); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 71 | |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 72 | // Read the hash table. |
| 73 | if (auto E = PublicsTable.read(Reader)) |
| 74 | return E; |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 75 | |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 76 | // Something called "address map" follows. |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame] | 77 | uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t); |
| 78 | if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 79 | return joinErrors(std::move(EC), |
| 80 | make_error<RawError>(raw_error_code::corrupt_file, |
| 81 | "Could not read an address map.")); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 82 | |
| 83 | // Something called "thunk map" follows. |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame] | 84 | if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 85 | return joinErrors(std::move(EC), |
| 86 | make_error<RawError>(raw_error_code::corrupt_file, |
| 87 | "Could not read a thunk map.")); |
Rui Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 88 | |
| 89 | // Something called "section map" follows. |
Zachary Turner | 349c18f | 2017-06-05 21:40:33 +0000 | [diff] [blame] | 90 | 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 Ueyama | 8dc18c5 | 2016-05-17 23:07:48 +0000 | [diff] [blame] | 96 | |
| 97 | if (Reader.bytesRemaining() > 0) |
| 98 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 99 | "Corrupted publics stream."); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 100 | return Error::success(); |
| 101 | } |