1148779dfSDimitry Andric //===- PDBStringTable.cpp - PDB String Table ---------------------*- C++-*-===//
2148779dfSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6148779dfSDimitry Andric //
7148779dfSDimitry Andric //===----------------------------------------------------------------------===//
8148779dfSDimitry Andric
9148779dfSDimitry Andric #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
10148779dfSDimitry Andric
11148779dfSDimitry Andric #include "llvm/DebugInfo/PDB/Native/Hash.h"
12148779dfSDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
13148779dfSDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
14148779dfSDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
15148779dfSDimitry Andric #include "llvm/Support/Endian.h"
16148779dfSDimitry Andric
17148779dfSDimitry Andric using namespace llvm;
18148779dfSDimitry Andric using namespace llvm::support;
19148779dfSDimitry Andric using namespace llvm::pdb;
20148779dfSDimitry Andric
getByteSize() const21ca089b24SDimitry Andric uint32_t PDBStringTable::getByteSize() const { return Header->ByteSize; }
getNameCount() const22148779dfSDimitry Andric uint32_t PDBStringTable::getNameCount() const { return NameCount; }
getHashVersion() const23148779dfSDimitry Andric uint32_t PDBStringTable::getHashVersion() const { return Header->HashVersion; }
getSignature() const24148779dfSDimitry Andric uint32_t PDBStringTable::getSignature() const { return Header->Signature; }
25148779dfSDimitry Andric
readHeader(BinaryStreamReader & Reader)26148779dfSDimitry Andric Error PDBStringTable::readHeader(BinaryStreamReader &Reader) {
27148779dfSDimitry Andric if (auto EC = Reader.readObject(Header))
28148779dfSDimitry Andric return EC;
29148779dfSDimitry Andric
30148779dfSDimitry Andric if (Header->Signature != PDBStringTableSignature)
31148779dfSDimitry Andric return make_error<RawError>(raw_error_code::corrupt_file,
32148779dfSDimitry Andric "Invalid hash table signature");
33148779dfSDimitry Andric if (Header->HashVersion != 1 && Header->HashVersion != 2)
34148779dfSDimitry Andric return make_error<RawError>(raw_error_code::corrupt_file,
35148779dfSDimitry Andric "Unsupported hash version");
36148779dfSDimitry Andric
37148779dfSDimitry Andric assert(Reader.bytesRemaining() == 0);
38148779dfSDimitry Andric return Error::success();
39148779dfSDimitry Andric }
40148779dfSDimitry Andric
readStrings(BinaryStreamReader & Reader)41148779dfSDimitry Andric Error PDBStringTable::readStrings(BinaryStreamReader &Reader) {
42148779dfSDimitry Andric BinaryStreamRef Stream;
43148779dfSDimitry Andric if (auto EC = Reader.readStreamRef(Stream))
44148779dfSDimitry Andric return EC;
45148779dfSDimitry Andric
46148779dfSDimitry Andric if (auto EC = Strings.initialize(Stream)) {
47148779dfSDimitry Andric return joinErrors(std::move(EC),
48148779dfSDimitry Andric make_error<RawError>(raw_error_code::corrupt_file,
49148779dfSDimitry Andric "Invalid hash table byte length"));
50148779dfSDimitry Andric }
51148779dfSDimitry Andric
52148779dfSDimitry Andric assert(Reader.bytesRemaining() == 0);
53148779dfSDimitry Andric return Error::success();
54148779dfSDimitry Andric }
55148779dfSDimitry Andric
567c7aba6eSDimitry Andric const codeview::DebugStringTableSubsectionRef &
getStringTable() const577c7aba6eSDimitry Andric PDBStringTable::getStringTable() const {
58d288ef4cSDimitry Andric return Strings;
59d288ef4cSDimitry Andric }
60d288ef4cSDimitry Andric
readHashTable(BinaryStreamReader & Reader)61148779dfSDimitry Andric Error PDBStringTable::readHashTable(BinaryStreamReader &Reader) {
62148779dfSDimitry Andric const support::ulittle32_t *HashCount;
63148779dfSDimitry Andric if (auto EC = Reader.readObject(HashCount))
64148779dfSDimitry Andric return EC;
65148779dfSDimitry Andric
66148779dfSDimitry Andric if (auto EC = Reader.readArray(IDs, *HashCount)) {
67148779dfSDimitry Andric return joinErrors(std::move(EC),
68148779dfSDimitry Andric make_error<RawError>(raw_error_code::corrupt_file,
69148779dfSDimitry Andric "Could not read bucket array"));
70148779dfSDimitry Andric }
71148779dfSDimitry Andric
72148779dfSDimitry Andric return Error::success();
73148779dfSDimitry Andric }
74148779dfSDimitry Andric
readEpilogue(BinaryStreamReader & Reader)75148779dfSDimitry Andric Error PDBStringTable::readEpilogue(BinaryStreamReader &Reader) {
76148779dfSDimitry Andric if (auto EC = Reader.readInteger(NameCount))
77148779dfSDimitry Andric return EC;
78148779dfSDimitry Andric
79148779dfSDimitry Andric assert(Reader.bytesRemaining() == 0);
80148779dfSDimitry Andric return Error::success();
81148779dfSDimitry Andric }
82148779dfSDimitry Andric
reload(BinaryStreamReader & Reader)83148779dfSDimitry Andric Error PDBStringTable::reload(BinaryStreamReader &Reader) {
84148779dfSDimitry Andric
85148779dfSDimitry Andric BinaryStreamReader SectionReader;
86148779dfSDimitry Andric
87148779dfSDimitry Andric std::tie(SectionReader, Reader) = Reader.split(sizeof(PDBStringTableHeader));
88148779dfSDimitry Andric if (auto EC = readHeader(SectionReader))
89148779dfSDimitry Andric return EC;
90148779dfSDimitry Andric
91148779dfSDimitry Andric std::tie(SectionReader, Reader) = Reader.split(Header->ByteSize);
92148779dfSDimitry Andric if (auto EC = readStrings(SectionReader))
93148779dfSDimitry Andric return EC;
94148779dfSDimitry Andric
95148779dfSDimitry Andric // We don't know how long the hash table is until we parse it, so let the
96148779dfSDimitry Andric // function responsible for doing that figure it out.
97148779dfSDimitry Andric if (auto EC = readHashTable(Reader))
98148779dfSDimitry Andric return EC;
99148779dfSDimitry Andric
100148779dfSDimitry Andric std::tie(SectionReader, Reader) = Reader.split(sizeof(uint32_t));
101148779dfSDimitry Andric if (auto EC = readEpilogue(SectionReader))
102148779dfSDimitry Andric return EC;
103148779dfSDimitry Andric
104148779dfSDimitry Andric assert(Reader.bytesRemaining() == 0);
105148779dfSDimitry Andric return Error::success();
106148779dfSDimitry Andric }
107148779dfSDimitry Andric
getStringForID(uint32_t ID) const108148779dfSDimitry Andric Expected<StringRef> PDBStringTable::getStringForID(uint32_t ID) const {
109148779dfSDimitry Andric return Strings.getString(ID);
110148779dfSDimitry Andric }
111148779dfSDimitry Andric
getIDForString(StringRef Str) const112148779dfSDimitry Andric Expected<uint32_t> PDBStringTable::getIDForString(StringRef Str) const {
113148779dfSDimitry Andric uint32_t Hash =
114148779dfSDimitry Andric (Header->HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str);
115148779dfSDimitry Andric size_t Count = IDs.size();
116148779dfSDimitry Andric uint32_t Start = Hash % Count;
117148779dfSDimitry Andric for (size_t I = 0; I < Count; ++I) {
118148779dfSDimitry Andric // The hash is just a starting point for the search, but if it
119148779dfSDimitry Andric // doesn't work we should find the string no matter what, because
120148779dfSDimitry Andric // we iterate the entire array.
121148779dfSDimitry Andric uint32_t Index = (Start + I) % Count;
122148779dfSDimitry Andric
123eb11fae6SDimitry Andric // If we find 0, it means the item isn't in the hash table.
124148779dfSDimitry Andric uint32_t ID = IDs[Index];
125eb11fae6SDimitry Andric if (ID == 0)
126eb11fae6SDimitry Andric return make_error<RawError>(raw_error_code::no_entry);
127148779dfSDimitry Andric auto ExpectedStr = getStringForID(ID);
128148779dfSDimitry Andric if (!ExpectedStr)
129148779dfSDimitry Andric return ExpectedStr.takeError();
130148779dfSDimitry Andric
131148779dfSDimitry Andric if (*ExpectedStr == Str)
132148779dfSDimitry Andric return ID;
133148779dfSDimitry Andric }
134148779dfSDimitry Andric return make_error<RawError>(raw_error_code::no_entry);
135148779dfSDimitry Andric }
136148779dfSDimitry Andric
name_ids() const137148779dfSDimitry Andric FixedStreamArray<support::ulittle32_t> PDBStringTable::name_ids() const {
138148779dfSDimitry Andric return IDs;
139148779dfSDimitry Andric }
140