1 //===- PDBStringTable.cpp - PDB String Table ---------------------*- C++-*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/DebugInfo/PDB/Native/Hash.h"
13 #include "llvm/DebugInfo/PDB/Native/RawError.h"
14 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
15 #include "llvm/Support/BinaryStreamReader.h"
16 #include "llvm/Support/Endian.h"
19 using namespace llvm::support
;
20 using namespace llvm::pdb
;
22 uint32_t PDBStringTable::getByteSize() const { return Header
->ByteSize
; }
23 uint32_t PDBStringTable::getNameCount() const { return NameCount
; }
24 uint32_t PDBStringTable::getHashVersion() const { return Header
->HashVersion
; }
25 uint32_t PDBStringTable::getSignature() const { return Header
->Signature
; }
27 Error
PDBStringTable::readHeader(BinaryStreamReader
&Reader
) {
28 if (auto EC
= Reader
.readObject(Header
))
31 if (Header
->Signature
!= PDBStringTableSignature
)
32 return make_error
<RawError
>(raw_error_code::corrupt_file
,
33 "Invalid hash table signature");
34 if (Header
->HashVersion
!= 1 && Header
->HashVersion
!= 2)
35 return make_error
<RawError
>(raw_error_code::corrupt_file
,
36 "Unsupported hash version");
38 assert(Reader
.bytesRemaining() == 0);
39 return Error::success();
42 Error
PDBStringTable::readStrings(BinaryStreamReader
&Reader
) {
43 BinaryStreamRef Stream
;
44 if (auto EC
= Reader
.readStreamRef(Stream
))
47 if (auto EC
= Strings
.initialize(Stream
)) {
48 return joinErrors(std::move(EC
),
49 make_error
<RawError
>(raw_error_code::corrupt_file
,
50 "Invalid hash table byte length"));
53 assert(Reader
.bytesRemaining() == 0);
54 return Error::success();
57 const codeview::DebugStringTableSubsectionRef
&
58 PDBStringTable::getStringTable() const {
62 Error
PDBStringTable::readHashTable(BinaryStreamReader
&Reader
) {
63 const support::ulittle32_t
*HashCount
;
64 if (auto EC
= Reader
.readObject(HashCount
))
67 if (auto EC
= Reader
.readArray(IDs
, *HashCount
)) {
68 return joinErrors(std::move(EC
),
69 make_error
<RawError
>(raw_error_code::corrupt_file
,
70 "Could not read bucket array"));
73 return Error::success();
76 Error
PDBStringTable::readEpilogue(BinaryStreamReader
&Reader
) {
77 if (auto EC
= Reader
.readInteger(NameCount
))
80 assert(Reader
.bytesRemaining() == 0);
81 return Error::success();
84 Error
PDBStringTable::reload(BinaryStreamReader
&Reader
) {
86 BinaryStreamReader SectionReader
;
88 std::tie(SectionReader
, Reader
) = Reader
.split(sizeof(PDBStringTableHeader
));
89 if (auto EC
= readHeader(SectionReader
))
92 std::tie(SectionReader
, Reader
) = Reader
.split(Header
->ByteSize
);
93 if (auto EC
= readStrings(SectionReader
))
96 // We don't know how long the hash table is until we parse it, so let the
97 // function responsible for doing that figure it out.
98 if (auto EC
= readHashTable(Reader
))
101 std::tie(SectionReader
, Reader
) = Reader
.split(sizeof(uint32_t));
102 if (auto EC
= readEpilogue(SectionReader
))
105 assert(Reader
.bytesRemaining() == 0);
106 return Error::success();
109 Expected
<StringRef
> PDBStringTable::getStringForID(uint32_t ID
) const {
110 return Strings
.getString(ID
);
113 Expected
<uint32_t> PDBStringTable::getIDForString(StringRef Str
) const {
115 (Header
->HashVersion
== 1) ? hashStringV1(Str
) : hashStringV2(Str
);
116 size_t Count
= IDs
.size();
117 uint32_t Start
= Hash
% Count
;
118 for (size_t I
= 0; I
< Count
; ++I
) {
119 // The hash is just a starting point for the search, but if it
120 // doesn't work we should find the string no matter what, because
121 // we iterate the entire array.
122 uint32_t Index
= (Start
+ I
) % Count
;
124 // If we find 0, it means the item isn't in the hash table.
125 uint32_t ID
= IDs
[Index
];
127 return make_error
<RawError
>(raw_error_code::no_entry
);
128 auto ExpectedStr
= getStringForID(ID
);
130 return ExpectedStr
.takeError();
132 if (*ExpectedStr
== Str
)
135 return make_error
<RawError
>(raw_error_code::no_entry
);
138 FixedStreamArray
<support::ulittle32_t
> PDBStringTable::name_ids() const {