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