blob: 3e22e7686bdb188457d149c0493e87d65d829c91 [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 Turnerbac69d32016-07-22 19:56:05 +000013#include "llvm/DebugInfo/Msf/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;
Zachary Turnerbac69d32016-07-22 19:56:05 +000019using namespace llvm::msf;
Zachary Turner0eace0b2016-05-02 18:09:14 +000020using namespace llvm::support;
21using namespace llvm::pdb;
22
Zachary Turner0eace0b2016-05-02 18:09:14 +000023NameHashTable::NameHashTable() : Signature(0), HashVersion(0), NameCount(0) {}
24
Zachary Turnerbac69d32016-07-22 19:56:05 +000025Error NameHashTable::load(StreamReader &Stream) {
Zachary Turner0eace0b2016-05-02 18:09:14 +000026 struct Header {
27 support::ulittle32_t Signature;
28 support::ulittle32_t HashVersion;
29 support::ulittle32_t ByteSize;
30 };
31
Zachary Turner8dbe3622016-05-27 01:54:44 +000032 const Header *H;
33 if (auto EC = Stream.readObject(H))
Zachary Turner819e77d2016-05-06 20:51:57 +000034 return EC;
35
Zachary Turner8dbe3622016-05-27 01:54:44 +000036 if (H->Signature != 0xEFFEEFFE)
Zachary Turner819e77d2016-05-06 20:51:57 +000037 return make_error<RawError>(raw_error_code::corrupt_file,
38 "Invalid hash table signature");
Zachary Turner8dbe3622016-05-27 01:54:44 +000039 if (H->HashVersion != 1 && H->HashVersion != 2)
Zachary Turner819e77d2016-05-06 20:51:57 +000040 return make_error<RawError>(raw_error_code::corrupt_file,
41 "Unsupported hash version");
Zachary Turner0eace0b2016-05-02 18:09:14 +000042
Zachary Turner8dbe3622016-05-27 01:54:44 +000043 Signature = H->Signature;
44 HashVersion = H->HashVersion;
45 if (auto EC = Stream.readStreamRef(NamesBuffer, H->ByteSize))
David Majnemer836937e2016-05-27 16:16:56 +000046 return joinErrors(std::move(EC),
47 make_error<RawError>(raw_error_code::corrupt_file,
48 "Invalid hash table byte length"));
Zachary Turner0eace0b2016-05-02 18:09:14 +000049
Zachary Turner8dbe3622016-05-27 01:54:44 +000050 const support::ulittle32_t *HashCount;
51 if (auto EC = Stream.readObject(HashCount))
Zachary Turner819e77d2016-05-06 20:51:57 +000052 return EC;
53
Zachary Turnerb393d952016-05-27 03:51:53 +000054 if (auto EC = Stream.readArray(IDs, *HashCount))
David Majnemer836937e2016-05-27 16:16:56 +000055 return joinErrors(std::move(EC),
56 make_error<RawError>(raw_error_code::corrupt_file,
57 "Could not read bucket array"));
Zachary Turner0eace0b2016-05-02 18:09:14 +000058
59 if (Stream.bytesRemaining() < sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +000060 return make_error<RawError>(raw_error_code::corrupt_file,
61 "Missing name count");
Zachary Turner0eace0b2016-05-02 18:09:14 +000062
Zachary Turner819e77d2016-05-06 20:51:57 +000063 if (auto EC = Stream.readInteger(NameCount))
64 return EC;
65 return Error::success();
Zachary Turner0eace0b2016-05-02 18:09:14 +000066}
67
68StringRef NameHashTable::getStringForID(uint32_t ID) const {
69 if (ID == IDs[0])
70 return StringRef();
71
Zachary Turner8dbe3622016-05-27 01:54:44 +000072 // NamesBuffer is a buffer of null terminated strings back to back. ID is
73 // the starting offset of the string we're looking for. So just seek into
74 // the desired offset and a read a null terminated stream from that offset.
75 StringRef Result;
Zachary Turnerbac69d32016-07-22 19:56:05 +000076 StreamReader NameReader(NamesBuffer);
Zachary Turner8dbe3622016-05-27 01:54:44 +000077 NameReader.setOffset(ID);
78 if (auto EC = NameReader.readZeroString(Result))
79 consumeError(std::move(EC));
80 return Result;
Zachary Turner0eace0b2016-05-02 18:09:14 +000081}
82
83uint32_t NameHashTable::getIDForString(StringRef Str) const {
Rui Ueyamaf05f3602016-06-08 23:15:09 +000084 uint32_t Hash = (HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str);
Zachary Turner0eace0b2016-05-02 18:09:14 +000085 size_t Count = IDs.size();
86 uint32_t Start = Hash % Count;
87 for (size_t I = 0; I < Count; ++I) {
88 // The hash is just a starting point for the search, but if it
89 // doesn't work we should find the string no matter what, because
90 // we iterate the entire array.
91 uint32_t Index = (Start + I) % Count;
92
93 uint32_t ID = IDs[Index];
94 StringRef S = getStringForID(ID);
95 if (S == Str)
96 return ID;
97 }
98 // IDs[0] contains the ID of the "invalid" entry.
99 return IDs[0];
100}
101
Zachary Turnerbac69d32016-07-22 19:56:05 +0000102FixedStreamArray<support::ulittle32_t> NameHashTable::name_ids() const {
Zachary Turnerb393d952016-05-27 03:51:53 +0000103 return IDs;
104}