blob: b182b70f006d6aa65c0928c3ccfc024c91bc611b [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"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000013#include "llvm/DebugInfo/CodeView/StreamReader.h"
Rui Ueyama170988f2016-06-08 23:11:14 +000014#include "llvm/DebugInfo/PDB/Raw/Hash.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000015#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000016#include "llvm/Support/Endian.h"
17
18using namespace llvm;
19using namespace llvm::support;
20using namespace llvm::pdb;
21
Zachary Turner0eace0b2016-05-02 18:09:14 +000022NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {}
23
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000024Error NameHashTable::load(codeview::StreamReader &Stream) {
Zachary Turner0eace0b2016-05-02 18:09:14 +000025 struct Header {
26 support::ulittle32_t Signature;
27 support::ulittle32_t HashVersion;
28 support::ulittle32_t ByteSize;
29 };
30
Zachary Turner8dbe3622016-05-27 01:54:44 +000031 const Header *H;
32 if (auto EC = Stream.readObject(H))
Zachary Turner819e77d2016-05-06 20:51:57 +000033 return EC;
34
Zachary Turner8dbe3622016-05-27 01:54:44 +000035 if (H->Signature != 0xEFFEEFFE)
Zachary Turner819e77d2016-05-06 20:51:57 +000036 return make_error<RawError>(raw_error_code::corrupt_file,
37 "Invalid hash table signature");
Zachary Turner8dbe3622016-05-27 01:54:44 +000038 if (H->HashVersion != 1 && H->HashVersion != 2)
Zachary Turner819e77d2016-05-06 20:51:57 +000039 return make_error<RawError>(raw_error_code::corrupt_file,
40 "Unsupported hash version");
Zachary Turner0eace0b2016-05-02 18:09:14 +000041
Zachary Turner8dbe3622016-05-27 01:54:44 +000042 Signature = H->Signature;
43 HashVersion = H->HashVersion;
44 if (auto EC = Stream.readStreamRef(NamesBuffer, H->ByteSize))
David Majnemer836937e2016-05-27 16:16:56 +000045 return joinErrors(std::move(EC),
46 make_error<RawError>(raw_error_code::corrupt_file,
47 "Invalid hash table byte length"));
Zachary Turner0eace0b2016-05-02 18:09:14 +000048
Zachary Turner8dbe3622016-05-27 01:54:44 +000049 const support::ulittle32_t *HashCount;
50 if (auto EC = Stream.readObject(HashCount))
Zachary Turner819e77d2016-05-06 20:51:57 +000051 return EC;
52
Zachary Turnerb393d952016-05-27 03:51:53 +000053 if (auto EC = Stream.readArray(IDs, *HashCount))
David Majnemer836937e2016-05-27 16:16:56 +000054 return joinErrors(std::move(EC),
55 make_error<RawError>(raw_error_code::corrupt_file,
56 "Could not read bucket array"));
Zachary Turner0eace0b2016-05-02 18:09:14 +000057
58 if (Stream.bytesRemaining() < sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000059 return make_error<RawError>(raw_error_code::corrupt_file,
60 "Missing name count");
Zachary Turner0eace0b2016-05-02 18:09:14 +000061
Zachary Turner819e77d2016-05-06 20:51:57 +000062 if (auto EC = Stream.readInteger(NameCount))
63 return EC;
64 return Error::success();
Zachary Turner0eace0b2016-05-02 18:09:14 +000065}
66
67StringRef NameHashTable::getStringForID(uint32_t ID) const {
68 if (ID == IDs[0])
69 return StringRef();
70
Zachary Turner8dbe3622016-05-27 01:54:44 +000071 // NamesBuffer is a buffer of null terminated strings back to back. ID is
72 // the starting offset of the string we're looking for. So just seek into
73 // the desired offset and a read a null terminated stream from that offset.
74 StringRef Result;
75 codeview::StreamReader NameReader(NamesBuffer);
76 NameReader.setOffset(ID);
77 if (auto EC = NameReader.readZeroString(Result))
78 consumeError(std::move(EC));
79 return Result;
Zachary Turner0eace0b2016-05-02 18:09:14 +000080}
81
82uint32_t NameHashTable::getIDForString(StringRef Str) const {
83 uint32_t Hash = (HashVersion == 1) ? HashStringV1(Str) : HashStringV2(Str);
84 size_t Count = IDs.size();
85 uint32_t Start = Hash % Count;
86 for (size_t I = 0; I < Count; ++I) {
87 // The hash is just a starting point for the search, but if it
88 // doesn't work we should find the string no matter what, because
89 // we iterate the entire array.
90 uint32_t Index = (Start + I) % Count;
91
92 uint32_t ID = IDs[Index];
93 StringRef S = getStringForID(ID);
94 if (S == Str)
95 return ID;
96 }
97 // IDs[0] contains the ID of the "invalid" entry.
98 return IDs[0];
99}
100
Zachary Turnerb393d952016-05-27 03:51:53 +0000101codeview::FixedStreamArray<support::ulittle32_t>
102NameHashTable::name_ids() const {
103 return IDs;
104}