Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 1 | //===- NameHashTable.cpp - PDB Name Hash Table ------------------*- C++ -*-===// |
| 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 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
| 11 | |
| 12 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 13 | #include "llvm/DebugInfo/Msf/StreamReader.h" |
Rui Ueyama | 170988f | 2016-06-08 23:11:14 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Raw/Hash.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
| 17 | |
| 18 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 19 | using namespace llvm::msf; |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 20 | using namespace llvm::support; |
| 21 | using namespace llvm::pdb; |
| 22 | |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 23 | NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {} |
| 24 | |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 25 | Error NameHashTable::load(StreamReader &Stream) { |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 26 | struct Header { |
| 27 | support::ulittle32_t Signature; |
| 28 | support::ulittle32_t HashVersion; |
| 29 | support::ulittle32_t ByteSize; |
| 30 | }; |
| 31 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 32 | const Header *H; |
| 33 | if (auto EC = Stream.readObject(H)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 34 | return EC; |
| 35 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 36 | if (H->Signature != 0xEFFEEFFE) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 37 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 38 | "Invalid hash table signature"); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 39 | if (H->HashVersion != 1 && H->HashVersion != 2) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 40 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 41 | "Unsupported hash version"); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 42 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 43 | Signature = H->Signature; |
| 44 | HashVersion = H->HashVersion; |
| 45 | if (auto EC = Stream.readStreamRef(NamesBuffer, H->ByteSize)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 46 | return joinErrors(std::move(EC), |
| 47 | make_error<RawError>(raw_error_code::corrupt_file, |
| 48 | "Invalid hash table byte length")); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 49 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 50 | const support::ulittle32_t *HashCount; |
| 51 | if (auto EC = Stream.readObject(HashCount)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 52 | return EC; |
| 53 | |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame] | 54 | if (auto EC = Stream.readArray(IDs, *HashCount)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 55 | return joinErrors(std::move(EC), |
| 56 | make_error<RawError>(raw_error_code::corrupt_file, |
| 57 | "Could not read bucket array")); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 58 | |
| 59 | if (Stream.bytesRemaining() < sizeof(support::ulittle32_t)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 60 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 61 | "Missing name count"); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 62 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 63 | if (auto EC = Stream.readInteger(NameCount)) |
| 64 | return EC; |
| 65 | return Error::success(); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | StringRef NameHashTable::getStringForID(uint32_t ID) const { |
| 69 | if (ID == IDs[0]) |
| 70 | return StringRef(); |
| 71 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 72 | // NamesBuffer is a buffer of null terminated strings back to back. ID is |
| 73 | // the starting offset of the string we're looking for. So just seek into |
| 74 | // the desired offset and a read a null terminated stream from that offset. |
| 75 | StringRef Result; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 76 | StreamReader NameReader(NamesBuffer); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 77 | NameReader.setOffset(ID); |
| 78 | if (auto EC = NameReader.readZeroString(Result)) |
| 79 | consumeError(std::move(EC)); |
| 80 | return Result; |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | uint32_t NameHashTable::getIDForString(StringRef Str) const { |
Rui Ueyama | f05f360 | 2016-06-08 23:15:09 +0000 | [diff] [blame] | 84 | uint32_t Hash = (HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str); |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 85 | size_t Count = IDs.size(); |
| 86 | uint32_t Start = Hash % Count; |
| 87 | for (size_t I = 0; I < Count; ++I) { |
| 88 | // The hash is just a starting point for the search, but if it |
| 89 | // doesn't work we should find the string no matter what, because |
| 90 | // we iterate the entire array. |
| 91 | uint32_t Index = (Start + I) % Count; |
| 92 | |
| 93 | uint32_t ID = IDs[Index]; |
| 94 | StringRef S = getStringForID(ID); |
| 95 | if (S == Str) |
| 96 | return ID; |
| 97 | } |
| 98 | // IDs[0] contains the ID of the "invalid" entry. |
| 99 | return IDs[0]; |
| 100 | } |
| 101 | |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame^] | 102 | FixedStreamArray<support::ulittle32_t> NameHashTable::name_ids() const { |
Zachary Turner | b393d95 | 2016-05-27 03:51:53 +0000 | [diff] [blame] | 103 | return IDs; |
| 104 | } |