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