blob: 1a468c436d82747fab17cf7b80e6b1de04a4303a [file] [log] [blame]
Zachary Turner0eace0b2016-05-02 18:09:14 +00001//===- NameHashTable.cpp - PDB Name Hash Table ------------------*- C++ -*-===//
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
10#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/DebugInfo/PDB/Raw/ByteStream.h"
14#include "llvm/DebugInfo/PDB/Raw/StreamReader.h"
15#include "llvm/Support/Endian.h"
16
17using namespace llvm;
18using namespace llvm::support;
19using namespace llvm::pdb;
20
21typedef uint32_t *PUL;
22typedef uint16_t *PUS;
23
24static inline uint32_t HashStringV1(StringRef Str) {
25 uint32_t Result = 0;
26 uint32_t Size = Str.size();
27
28 ArrayRef<ulittle32_t> Longs(reinterpret_cast<const ulittle32_t *>(Str.data()),
29 Size / 4);
30
31 for (auto Value : Longs)
32 Result ^= Value;
33
34 const uint8_t *Remainder = reinterpret_cast<const uint8_t *>(Longs.end());
35 uint32_t RemainderSize = Size - Longs.size() * 4;
36
37 // Maximum of 3 bytes left. Hash a 2 byte word if possible, then hash the
38 // possibly remaining 1 byte.
39 if (RemainderSize >= 2) {
Zachary Turnera801dc12016-05-02 18:36:58 +000040 uint16_t Value = *reinterpret_cast<const ulittle16_t *>(Remainder);
41 Result ^= static_cast<uint32_t>(Value);
Zachary Turner0eace0b2016-05-02 18:09:14 +000042 Remainder += 2;
43 RemainderSize -= 2;
44 }
45
46 // hash possible odd byte
47 if (RemainderSize == 1) {
48 Result ^= *(Remainder++);
49 }
50
51 const uint32_t toLowerMask = 0x20202020;
52 Result |= toLowerMask;
53 Result ^= (Result >> 11);
54
55 return Result ^ (Result >> 16);
56}
57
58static inline uint32_t HashStringV2(StringRef Str) {
59 uint32_t Hash = 0xb170a1bf;
60
61 ArrayRef<char> Buffer(Str.begin(), Str.end());
62
63 ArrayRef<ulittle32_t> Items(
64 reinterpret_cast<const ulittle32_t *>(Buffer.data()),
65 Buffer.size() / sizeof(ulittle32_t));
66 for (ulittle32_t Item : Items) {
67 Hash += Item;
68 Hash += (Hash << 10);
69 Hash ^= (Hash >> 6);
70 }
71 Buffer = Buffer.slice(Items.size() * sizeof(ulittle32_t));
72 for (uint8_t Item : Buffer) {
73 Hash += Item;
74 Hash += (Hash << 10);
75 Hash ^= (Hash >> 6);
76 }
77
78 return Hash * 1664525L + 1013904223L;
79}
80
81NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {}
82
83std::error_code NameHashTable::load(StreamReader &Stream) {
84 struct Header {
85 support::ulittle32_t Signature;
86 support::ulittle32_t HashVersion;
87 support::ulittle32_t ByteSize;
88 };
89
90 Header H;
91 Stream.readObject(&H);
92 if (H.Signature != 0xEFFEEFFE)
93 return std::make_error_code(std::errc::illegal_byte_sequence);
94 if (H.HashVersion != 1 && H.HashVersion != 2)
95 return std::make_error_code(std::errc::not_supported);
96
97 Signature = H.Signature;
98 HashVersion = H.HashVersion;
99 NamesBuffer.initialize(Stream, H.ByteSize);
100
101 support::ulittle32_t HashCount;
102 Stream.readObject(&HashCount);
103 std::vector<support::ulittle32_t> BucketArray(HashCount);
104 Stream.readArray<support::ulittle32_t>(BucketArray);
105 IDs.assign(BucketArray.begin(), BucketArray.end());
106
107 if (Stream.bytesRemaining() < sizeof(support::ulittle32_t))
108 return std::make_error_code(std::errc::illegal_byte_sequence);
109
110 Stream.readInteger(NameCount);
111 return std::error_code();
112}
113
114StringRef NameHashTable::getStringForID(uint32_t ID) const {
115 if (ID == IDs[0])
116 return StringRef();
117
118 return StringRef(NamesBuffer.str().begin() + ID);
119}
120
121uint32_t NameHashTable::getIDForString(StringRef Str) const {
122 uint32_t Hash = (HashVersion == 1) ? HashStringV1(Str) : HashStringV2(Str);
123 size_t Count = IDs.size();
124 uint32_t Start = Hash % Count;
125 for (size_t I = 0; I < Count; ++I) {
126 // The hash is just a starting point for the search, but if it
127 // doesn't work we should find the string no matter what, because
128 // we iterate the entire array.
129 uint32_t Index = (Start + I) % Count;
130
131 uint32_t ID = IDs[Index];
132 StringRef S = getStringForID(ID);
133 if (S == Str)
134 return ID;
135 }
136 // IDs[0] contains the ID of the "invalid" entry.
137 return IDs[0];
138}
139
140ArrayRef<uint32_t> NameHashTable::name_ids() const {
141 return ArrayRef<uint32_t>(IDs).slice(1, NameCount);
142}