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