xref: /aosp_15_r20/external/llvm/lib/DebugInfo/PDB/Raw/NameHashTable.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- NameHashTable.cpp - PDB Name Hash Table ------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
11*9880d681SAndroid Build Coastguard Worker 
12*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/ArrayRef.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/CodeView/StreamReader.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/PDB/Raw/Hash.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/DebugInfo/PDB/Raw/RawError.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Endian.h"
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker using namespace llvm;
19*9880d681SAndroid Build Coastguard Worker using namespace llvm::support;
20*9880d681SAndroid Build Coastguard Worker using namespace llvm::pdb;
21*9880d681SAndroid Build Coastguard Worker 
NameHashTable()22*9880d681SAndroid Build Coastguard Worker NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {}
23*9880d681SAndroid Build Coastguard Worker 
load(codeview::StreamReader & Stream)24*9880d681SAndroid Build Coastguard Worker Error NameHashTable::load(codeview::StreamReader &Stream) {
25*9880d681SAndroid Build Coastguard Worker   struct Header {
26*9880d681SAndroid Build Coastguard Worker     support::ulittle32_t Signature;
27*9880d681SAndroid Build Coastguard Worker     support::ulittle32_t HashVersion;
28*9880d681SAndroid Build Coastguard Worker     support::ulittle32_t ByteSize;
29*9880d681SAndroid Build Coastguard Worker   };
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker   const Header *H;
32*9880d681SAndroid Build Coastguard Worker   if (auto EC = Stream.readObject(H))
33*9880d681SAndroid Build Coastguard Worker     return EC;
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker   if (H->Signature != 0xEFFEEFFE)
36*9880d681SAndroid Build Coastguard Worker     return make_error<RawError>(raw_error_code::corrupt_file,
37*9880d681SAndroid Build Coastguard Worker                                 "Invalid hash table signature");
38*9880d681SAndroid Build Coastguard Worker   if (H->HashVersion != 1 && H->HashVersion != 2)
39*9880d681SAndroid Build Coastguard Worker     return make_error<RawError>(raw_error_code::corrupt_file,
40*9880d681SAndroid Build Coastguard Worker                                 "Unsupported hash version");
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker   Signature = H->Signature;
43*9880d681SAndroid Build Coastguard Worker   HashVersion = H->HashVersion;
44*9880d681SAndroid Build Coastguard Worker   if (auto EC = Stream.readStreamRef(NamesBuffer, H->ByteSize))
45*9880d681SAndroid Build Coastguard Worker     return joinErrors(std::move(EC),
46*9880d681SAndroid Build Coastguard Worker                       make_error<RawError>(raw_error_code::corrupt_file,
47*9880d681SAndroid Build Coastguard Worker                                            "Invalid hash table byte length"));
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   const support::ulittle32_t *HashCount;
50*9880d681SAndroid Build Coastguard Worker   if (auto EC = Stream.readObject(HashCount))
51*9880d681SAndroid Build Coastguard Worker     return EC;
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   if (auto EC = Stream.readArray(IDs, *HashCount))
54*9880d681SAndroid Build Coastguard Worker     return joinErrors(std::move(EC),
55*9880d681SAndroid Build Coastguard Worker                       make_error<RawError>(raw_error_code::corrupt_file,
56*9880d681SAndroid Build Coastguard Worker                                            "Could not read bucket array"));
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker   if (Stream.bytesRemaining() < sizeof(support::ulittle32_t))
59*9880d681SAndroid Build Coastguard Worker     return make_error<RawError>(raw_error_code::corrupt_file,
60*9880d681SAndroid Build Coastguard Worker                                 "Missing name count");
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker   if (auto EC = Stream.readInteger(NameCount))
63*9880d681SAndroid Build Coastguard Worker     return EC;
64*9880d681SAndroid Build Coastguard Worker   return Error::success();
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker 
getStringForID(uint32_t ID) const67*9880d681SAndroid Build Coastguard Worker StringRef NameHashTable::getStringForID(uint32_t ID) const {
68*9880d681SAndroid Build Coastguard Worker   if (ID == IDs[0])
69*9880d681SAndroid Build Coastguard Worker     return StringRef();
70*9880d681SAndroid Build Coastguard Worker 
71*9880d681SAndroid Build Coastguard Worker   // NamesBuffer is a buffer of null terminated strings back to back.  ID is
72*9880d681SAndroid Build Coastguard Worker   // the starting offset of the string we're looking for.  So just seek into
73*9880d681SAndroid Build Coastguard Worker   // the desired offset and a read a null terminated stream from that offset.
74*9880d681SAndroid Build Coastguard Worker   StringRef Result;
75*9880d681SAndroid Build Coastguard Worker   codeview::StreamReader NameReader(NamesBuffer);
76*9880d681SAndroid Build Coastguard Worker   NameReader.setOffset(ID);
77*9880d681SAndroid Build Coastguard Worker   if (auto EC = NameReader.readZeroString(Result))
78*9880d681SAndroid Build Coastguard Worker     consumeError(std::move(EC));
79*9880d681SAndroid Build Coastguard Worker   return Result;
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker 
getIDForString(StringRef Str) const82*9880d681SAndroid Build Coastguard Worker uint32_t NameHashTable::getIDForString(StringRef Str) const {
83*9880d681SAndroid Build Coastguard Worker   uint32_t Hash = (HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str);
84*9880d681SAndroid Build Coastguard Worker   size_t Count = IDs.size();
85*9880d681SAndroid Build Coastguard Worker   uint32_t Start = Hash % Count;
86*9880d681SAndroid Build Coastguard Worker   for (size_t I = 0; I < Count; ++I) {
87*9880d681SAndroid Build Coastguard Worker     // The hash is just a starting point for the search, but if it
88*9880d681SAndroid Build Coastguard Worker     // doesn't work we should find the string no matter what, because
89*9880d681SAndroid Build Coastguard Worker     // we iterate the entire array.
90*9880d681SAndroid Build Coastguard Worker     uint32_t Index = (Start + I) % Count;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker     uint32_t ID = IDs[Index];
93*9880d681SAndroid Build Coastguard Worker     StringRef S = getStringForID(ID);
94*9880d681SAndroid Build Coastguard Worker     if (S == Str)
95*9880d681SAndroid Build Coastguard Worker       return ID;
96*9880d681SAndroid Build Coastguard Worker   }
97*9880d681SAndroid Build Coastguard Worker   // IDs[0] contains the ID of the "invalid" entry.
98*9880d681SAndroid Build Coastguard Worker   return IDs[0];
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker 
101*9880d681SAndroid Build Coastguard Worker codeview::FixedStreamArray<support::ulittle32_t>
name_ids() const102*9880d681SAndroid Build Coastguard Worker NameHashTable::name_ids() const {
103*9880d681SAndroid Build Coastguard Worker   return IDs;
104*9880d681SAndroid Build Coastguard Worker }
105