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