Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 1 | //===- PDBStringTable.cpp - PDB String Table ---------------------*- C++-*-===// |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 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 |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 9 | #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h" |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 10 | |
| 11 | #include "llvm/ADT/ArrayRef.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/Native/Hash.h" |
| 13 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
| 14 | #include "llvm/DebugInfo/PDB/Native/RawTypes.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 15 | #include "llvm/Support/BinaryStreamReader.h" |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::support; |
| 20 | using namespace llvm::pdb; |
| 21 | |
Zachary Turner | f3b4b2d | 2017-07-07 18:45:37 +0000 | [diff] [blame] | 22 | uint32_t PDBStringTable::getByteSize() const { return Header->ByteSize; } |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 23 | uint32_t PDBStringTable::getNameCount() const { return NameCount; } |
| 24 | uint32_t PDBStringTable::getHashVersion() const { return Header->HashVersion; } |
| 25 | uint32_t PDBStringTable::getSignature() const { return Header->Signature; } |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 26 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 27 | Error PDBStringTable::readHeader(BinaryStreamReader &Reader) { |
| 28 | if (auto EC = Reader.readObject(Header)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 29 | return EC; |
| 30 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 31 | if (Header->Signature != PDBStringTableSignature) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 32 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 33 | "Invalid hash table signature"); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 34 | if (Header->HashVersion != 1 && Header->HashVersion != 2) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 35 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 36 | "Unsupported hash version"); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 37 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 38 | assert(Reader.bytesRemaining() == 0); |
| 39 | return Error::success(); |
| 40 | } |
| 41 | |
| 42 | Error PDBStringTable::readStrings(BinaryStreamReader &Reader) { |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 43 | BinaryStreamRef Stream; |
| 44 | if (auto EC = Reader.readStreamRef(Stream)) |
| 45 | return EC; |
| 46 | |
| 47 | if (auto EC = Strings.initialize(Stream)) { |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 48 | return joinErrors(std::move(EC), |
| 49 | make_error<RawError>(raw_error_code::corrupt_file, |
| 50 | "Invalid hash table byte length")); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 51 | } |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 52 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 53 | assert(Reader.bytesRemaining() == 0); |
| 54 | return Error::success(); |
| 55 | } |
| 56 | |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 57 | const codeview::DebugStringTableSubsectionRef & |
| 58 | PDBStringTable::getStringTable() const { |
Zachary Turner | 92dcdda | 2017-06-02 19:49:14 +0000 | [diff] [blame] | 59 | return Strings; |
| 60 | } |
| 61 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 62 | Error PDBStringTable::readHashTable(BinaryStreamReader &Reader) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 63 | const support::ulittle32_t *HashCount; |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 64 | if (auto EC = Reader.readObject(HashCount)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 65 | return EC; |
| 66 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 67 | if (auto EC = Reader.readArray(IDs, *HashCount)) { |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 68 | return joinErrors(std::move(EC), |
| 69 | make_error<RawError>(raw_error_code::corrupt_file, |
| 70 | "Could not read bucket array")); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 71 | } |
Zachary Turner | f122008 | 2017-03-15 22:19:30 +0000 | [diff] [blame] | 72 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 73 | return Error::success(); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 76 | Error PDBStringTable::readEpilogue(BinaryStreamReader &Reader) { |
| 77 | if (auto EC = Reader.readInteger(NameCount)) |
| 78 | return EC; |
| 79 | |
| 80 | assert(Reader.bytesRemaining() == 0); |
| 81 | return Error::success(); |
| 82 | } |
| 83 | |
| 84 | Error PDBStringTable::reload(BinaryStreamReader &Reader) { |
| 85 | |
| 86 | BinaryStreamReader SectionReader; |
| 87 | |
| 88 | std::tie(SectionReader, Reader) = Reader.split(sizeof(PDBStringTableHeader)); |
| 89 | if (auto EC = readHeader(SectionReader)) |
| 90 | return EC; |
| 91 | |
| 92 | std::tie(SectionReader, Reader) = Reader.split(Header->ByteSize); |
| 93 | if (auto EC = readStrings(SectionReader)) |
| 94 | return EC; |
| 95 | |
| 96 | // We don't know how long the hash table is until we parse it, so let the |
| 97 | // function responsible for doing that figure it out. |
| 98 | if (auto EC = readHashTable(Reader)) |
| 99 | return EC; |
| 100 | |
| 101 | std::tie(SectionReader, Reader) = Reader.split(sizeof(uint32_t)); |
| 102 | if (auto EC = readEpilogue(SectionReader)) |
| 103 | return EC; |
| 104 | |
| 105 | assert(Reader.bytesRemaining() == 0); |
| 106 | return Error::success(); |
| 107 | } |
Zachary Turner | f122008 | 2017-03-15 22:19:30 +0000 | [diff] [blame] | 108 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 109 | Expected<StringRef> PDBStringTable::getStringForID(uint32_t ID) const { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 110 | return Strings.getString(ID); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 113 | Expected<uint32_t> PDBStringTable::getIDForString(StringRef Str) const { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 114 | uint32_t Hash = |
| 115 | (Header->HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 116 | size_t Count = IDs.size(); |
| 117 | uint32_t Start = Hash % Count; |
| 118 | for (size_t I = 0; I < Count; ++I) { |
| 119 | // The hash is just a starting point for the search, but if it |
| 120 | // doesn't work we should find the string no matter what, because |
| 121 | // we iterate the entire array. |
| 122 | uint32_t Index = (Start + I) % Count; |
| 123 | |
Zachary Turner | eb62999 | 2018-03-21 22:23:59 +0000 | [diff] [blame] | 124 | // If we find 0, it means the item isn't in the hash table. |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 125 | uint32_t ID = IDs[Index]; |
Zachary Turner | eb62999 | 2018-03-21 22:23:59 +0000 | [diff] [blame] | 126 | if (ID == 0) |
| 127 | return make_error<RawError>(raw_error_code::no_entry); |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 128 | auto ExpectedStr = getStringForID(ID); |
| 129 | if (!ExpectedStr) |
| 130 | return ExpectedStr.takeError(); |
| 131 | |
| 132 | if (*ExpectedStr == Str) |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 133 | return ID; |
| 134 | } |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 135 | return make_error<RawError>(raw_error_code::no_entry); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 138 | FixedStreamArray<support::ulittle32_t> PDBStringTable::name_ids() const { |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame] | 139 | return IDs; |
| 140 | } |