blob: 3e8de73609f1a0802b4b9a6fdf9349e0248905d3 [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) {
40 Result ^= *reinterpret_cast<const ulittle16_t *>(Remainder);
41 Remainder += 2;
42 RemainderSize -= 2;
43 }
44
45 // hash possible odd byte
46 if (RemainderSize == 1) {
47 Result ^= *(Remainder++);
48 }
49
50 const uint32_t toLowerMask = 0x20202020;
51 Result |= toLowerMask;
52 Result ^= (Result >> 11);
53
54 return Result ^ (Result >> 16);
55}
56
57static inline uint32_t HashStringV2(StringRef Str) {
58 uint32_t Hash = 0xb170a1bf;
59
60 ArrayRef<char> Buffer(Str.begin(), Str.end());
61
62 ArrayRef<ulittle32_t> Items(
63 reinterpret_cast<const ulittle32_t *>(Buffer.data()),
64 Buffer.size() / sizeof(ulittle32_t));
65 for (ulittle32_t Item : Items) {
66 Hash += Item;
67 Hash += (Hash << 10);
68 Hash ^= (Hash >> 6);
69 }
70 Buffer = Buffer.slice(Items.size() * sizeof(ulittle32_t));
71 for (uint8_t Item : Buffer) {
72 Hash += Item;
73 Hash += (Hash << 10);
74 Hash ^= (Hash >> 6);
75 }
76
77 return Hash * 1664525L + 1013904223L;
78}
79
80NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {}
81
82std::error_code NameHashTable::load(StreamReader &Stream) {
83 struct Header {
84 support::ulittle32_t Signature;
85 support::ulittle32_t HashVersion;
86 support::ulittle32_t ByteSize;
87 };
88
89 Header H;
90 Stream.readObject(&H);
91 if (H.Signature != 0xEFFEEFFE)
92 return std::make_error_code(std::errc::illegal_byte_sequence);
93 if (H.HashVersion != 1 && H.HashVersion != 2)
94 return std::make_error_code(std::errc::not_supported);
95
96 Signature = H.Signature;
97 HashVersion = H.HashVersion;
98 NamesBuffer.initialize(Stream, H.ByteSize);
99
100 support::ulittle32_t HashCount;
101 Stream.readObject(&HashCount);
102 std::vector<support::ulittle32_t> BucketArray(HashCount);
103 Stream.readArray<support::ulittle32_t>(BucketArray);
104 IDs.assign(BucketArray.begin(), BucketArray.end());
105
106 if (Stream.bytesRemaining() < sizeof(support::ulittle32_t))
107 return std::make_error_code(std::errc::illegal_byte_sequence);
108
109 Stream.readInteger(NameCount);
110 return std::error_code();
111}
112
113StringRef NameHashTable::getStringForID(uint32_t ID) const {
114 if (ID == IDs[0])
115 return StringRef();
116
117 return StringRef(NamesBuffer.str().begin() + ID);
118}
119
120uint32_t NameHashTable::getIDForString(StringRef Str) const {
121 uint32_t Hash = (HashVersion == 1) ? HashStringV1(Str) : HashStringV2(Str);
122 size_t Count = IDs.size();
123 uint32_t Start = Hash % Count;
124 for (size_t I = 0; I < Count; ++I) {
125 // The hash is just a starting point for the search, but if it
126 // doesn't work we should find the string no matter what, because
127 // we iterate the entire array.
128 uint32_t Index = (Start + I) % Count;
129
130 uint32_t ID = IDs[Index];
131 StringRef S = getStringForID(ID);
132 if (S == Str)
133 return ID;
134 }
135 // IDs[0] contains the ID of the "invalid" entry.
136 return IDs[0];
137}
138
139ArrayRef<uint32_t> NameHashTable::name_ids() const {
140 return ArrayRef<uint32_t>(IDs).slice(1, NameCount);
141}