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