blob: ed469317ee8ccdb7518b5706567ad9e72585e953 [file] [log] [blame]
Zachary Turnerf34e0162016-04-26 16:20:00 +00001//===- PDBNameMap.cpp - PDB Name Map ----------------------------*- 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/PDBNameMap.h"
11#include "llvm/ADT/BitVector.h"
12#include "llvm/DebugInfo/PDB/Raw/PDBStream.h"
13
14using namespace llvm;
15
16PDBNameMap::PDBNameMap() {}
17
18std::error_code PDBNameMap::load(PDBStream &Stream) {
19 // This is some sort of weird string-set/hash table encoded in the stream.
20 // It starts with the number of bytes in the table.
21 uint32_t NumberOfBytes;
22 Stream.readInteger(NumberOfBytes);
23
24 // Following that field is the starting offset of strings in the name table.
25 uint32_t StringsOffset = Stream.getOffset();
26 Stream.setOffset(StringsOffset + NumberOfBytes);
27
28 // This appears to be equivalent to the total number of strings *actually*
29 // in the name table.
30 uint32_t HashSize;
31 Stream.readInteger(HashSize);
32
33 // This appears to be an upper bound on the number of strings in the name
34 // table.
35 uint32_t MaxNumberOfStrings;
36 Stream.readInteger(MaxNumberOfStrings);
37
38 // This appears to be a hash table which uses bitfields to determine whether
39 // or not a bucket is 'present'.
40 uint32_t NumPresentWords;
41 Stream.readInteger(NumPresentWords);
42
43 // Store all the 'present' bits in a vector for later processing.
44 SmallVector<uint32_t, 1> PresentWords;
45 for (uint32_t I = 0; I != NumPresentWords; ++I) {
46 uint32_t Word;
47 Stream.readInteger(Word);
48 PresentWords.push_back(Word);
49 }
50
51 // This appears to be a hash table which uses bitfields to determine whether
52 // or not a bucket is 'deleted'.
53 uint32_t NumDeletedWords;
54 Stream.readInteger(NumDeletedWords);
55
56 // Store all the 'deleted' bits in a vector for later processing.
57 SmallVector<uint32_t, 1> DeletedWords;
58 for (uint32_t I = 0; I != NumDeletedWords; ++I) {
59 uint32_t Word;
60 Stream.readInteger(Word);
61 DeletedWords.push_back(Word);
62 }
63
64 BitVector Present(MaxNumberOfStrings, false);
65 if (!PresentWords.empty())
66 Present.setBitsInMask(PresentWords.data(), PresentWords.size());
67 BitVector Deleted(MaxNumberOfStrings, false);
68 if (!DeletedWords.empty())
69 Deleted.setBitsInMask(DeletedWords.data(), DeletedWords.size());
70
71 for (uint32_t I = 0; I < MaxNumberOfStrings; ++I) {
72 if (!Present.test(I))
73 continue;
74
75 // For all present entries, dump out their mapping.
76
77 // This appears to be an offset relative to the start of the strings.
78 // It tells us where the null-terminated string begins.
79 uint32_t NameOffset;
80 Stream.readInteger(NameOffset);
81
82 // This appears to be a stream number into the stream directory.
83 uint32_t NameIndex;
84 Stream.readInteger(NameIndex);
85
86 // Compute the offset of the start of the string relative to the stream.
87 uint32_t StringOffset = StringsOffset + NameOffset;
88 uint32_t OldOffset = Stream.getOffset();
89 // Pump out our c-string from the stream.
90 std::string Str;
91 Stream.setOffset(StringOffset);
92 Stream.readZeroString(Str);
93
94 Stream.setOffset(OldOffset);
95 // Add this to a string-map from name to stream number.
96 Mapping.insert({Str, NameIndex});
97 }
98
99 return std::error_code();
100}
101
102bool PDBNameMap::tryGetValue(StringRef Name, uint32_t &Value) const {
103 auto Iter = Mapping.find(Name);
104 if (Iter == Mapping.end())
105 return false;
106 Value = Iter->second;
107 return true;
108}