blob: d9dcabf3d958588e152df3bc71079752cac75598 [file] [log] [blame]
Zachary Turnere204a6c2017-05-02 18:00:13 +00001//===- PDBStringTableBuilder.cpp - PDB String Table -------------*- C++ -*-===//
Rui Ueyamadcd32932017-01-15 00:36:02 +00002//
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
Zachary Turnere204a6c2017-05-02 18:00:13 +000010#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Zachary Turnerc504ae32017-05-03 15:58:37 +000011
Rui Ueyamadcd32932017-01-15 00:36:02 +000012#include "llvm/ADT/ArrayRef.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000013#include "llvm/DebugInfo/PDB/Native/Hash.h"
14#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000015#include "llvm/Support/BinaryStreamWriter.h"
Rui Ueyamadcd32932017-01-15 00:36:02 +000016#include "llvm/Support/Endian.h"
17
Zachary Turnera6fb5362018-03-23 18:43:39 +000018#include <map>
19
Rui Ueyamadcd32932017-01-15 00:36:02 +000020using namespace llvm;
Zachary Turnerc504ae32017-05-03 15:58:37 +000021using namespace llvm::msf;
Rui Ueyamadcd32932017-01-15 00:36:02 +000022using namespace llvm::support;
23using namespace llvm::support::endian;
24using namespace llvm::pdb;
25
Zachary Turnerf2282762018-03-23 19:57:25 +000026StringTableHashTraits::StringTableHashTraits(PDBStringTableBuilder &Table)
27 : Table(&Table) {}
28
29uint32_t StringTableHashTraits::hashLookupKey(StringRef S) const {
30 return Table->getIdForString(S);
31}
32
33StringRef StringTableHashTraits::storageKeyToLookupKey(uint32_t Offset) const {
34 return Table->getStringForId(Offset);
35}
36
37uint32_t StringTableHashTraits::lookupKeyToStorageKey(StringRef S) {
38 return Table->insert(S);
39}
40
Zachary Turnere204a6c2017-05-02 18:00:13 +000041uint32_t PDBStringTableBuilder::insert(StringRef S) {
Zachary Turnerc504ae32017-05-03 15:58:37 +000042 return Strings.insert(S);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000043}
44
Zachary Turner71d36ad2018-03-22 17:37:28 +000045uint32_t PDBStringTableBuilder::getIdForString(StringRef S) const {
46 return Strings.getIdForString(S);
47}
48
49StringRef PDBStringTableBuilder::getStringForId(uint32_t Id) const {
50 return Strings.getStringForId(Id);
51}
52
Zachary Turnera6fb5362018-03-23 18:43:39 +000053// This is a precomputed list of Buckets given the specified number of
54// strings. Matching the reference algorithm exactly is not strictly
55// necessary for correctness, but it helps when comparing LLD's PDBs with
56// Microsoft's PDBs so as to eliminate superfluous differences.
57static std::map<uint32_t, uint32_t> StringsToBuckets = {
58 {1, 2},
59 {2, 4},
60 {4, 7},
61 {6, 11},
62 {9, 17},
63 {13, 26},
64 {20, 40},
65 {31, 61},
66 {46, 92},
67 {70, 139},
68 {105, 209},
69 {157, 314},
70 {236, 472},
71 {355, 709},
72 {532, 1064},
73 {799, 1597},
74 {1198, 2396},
75 {1798, 3595},
76 {2697, 5393},
77 {4045, 8090},
78 {6068, 12136},
79 {9103, 18205},
80 {13654, 27308},
81 {20482, 40963},
82 {30723, 61445},
83 {46084, 92168},
84 {69127, 138253},
85 {103690, 207380},
86 {155536, 311071},
87 {233304, 466607},
88 {349956, 699911},
89 {524934, 1049867},
90 {787401, 1574801},
91 {1181101, 2362202},
92 {1771652, 3543304},
93 {2657479, 5314957},
94 {3986218, 7972436},
95 {5979328, 11958655},
96 {8968992, 17937983},
97 {13453488, 26906975},
98 {20180232, 40360463},
99 {30270348, 60540695},
100 {45405522, 90811043},
101 {68108283, 136216565},
102 {102162424, 204324848},
103 {153243637, 306487273},
104 {229865455, 459730910},
105 {344798183, 689596366},
106 {517197275, 1034394550},
107 {775795913, 1551591826}};
108
Rui Ueyamadcd32932017-01-15 00:36:02 +0000109static uint32_t computeBucketCount(uint32_t NumStrings) {
Zachary Turnera6fb5362018-03-23 18:43:39 +0000110 auto Entry = StringsToBuckets.lower_bound(NumStrings);
111 assert(Entry != StringsToBuckets.end());
112 return Entry->second;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000113}
114
Zachary Turnerc504ae32017-05-03 15:58:37 +0000115uint32_t PDBStringTableBuilder::calculateHashTableSize() const {
116 uint32_t Size = sizeof(uint32_t); // Hash table begins with 4-byte size field.
117 Size += sizeof(uint32_t) * computeBucketCount(Strings.size());
Daniel Jasperdff096f2017-05-03 07:29:25 +0000118
Zachary Turner7dba20b2017-05-02 23:36:17 +0000119 return Size;
120}
121
Zachary Turnerc504ae32017-05-03 15:58:37 +0000122uint32_t PDBStringTableBuilder::calculateSerializedSize() const {
123 uint32_t Size = 0;
124 Size += sizeof(PDBStringTableHeader);
125 Size += Strings.calculateSerializedSize();
126 Size += calculateHashTableSize();
127 Size += sizeof(uint32_t); // The /names stream ends with the string count.
128 return Size;
129}
130
Zachary Turnera8cfc292017-06-14 15:59:27 +0000131void PDBStringTableBuilder::setStrings(
132 const codeview::DebugStringTableSubsection &Strings) {
133 this->Strings = Strings;
134}
135
Zachary Turnerc504ae32017-05-03 15:58:37 +0000136Error PDBStringTableBuilder::writeHeader(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +0000137 // Write a header
Zachary Turnere204a6c2017-05-02 18:00:13 +0000138 PDBStringTableHeader H;
139 H.Signature = PDBStringTableSignature;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000140 H.HashVersion = 1;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000141 H.ByteSize = Strings.calculateSerializedSize();
Rui Ueyamadcd32932017-01-15 00:36:02 +0000142 if (auto EC = Writer.writeObject(H))
143 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000144 assert(Writer.bytesRemaining() == 0);
145 return Error::success();
146}
Rui Ueyamadcd32932017-01-15 00:36:02 +0000147
Zachary Turnerc504ae32017-05-03 15:58:37 +0000148Error PDBStringTableBuilder::writeStrings(BinaryStreamWriter &Writer) const {
149 if (auto EC = Strings.commit(Writer))
150 return EC;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000151
Zachary Turnerc504ae32017-05-03 15:58:37 +0000152 assert(Writer.bytesRemaining() == 0);
153 return Error::success();
154}
155
156Error PDBStringTableBuilder::writeHashTable(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +0000157 // Write a hash table.
158 uint32_t BucketCount = computeBucketCount(Strings.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000159 if (auto EC = Writer.writeInteger(BucketCount))
Rui Ueyamadcd32932017-01-15 00:36:02 +0000160 return EC;
161 std::vector<ulittle32_t> Buckets(BucketCount);
162
Zachary Turnerc504ae32017-05-03 15:58:37 +0000163 for (auto &Pair : Strings) {
164 StringRef S = Pair.getKey();
165 uint32_t Offset = Pair.getValue();
Rui Ueyamadcd32932017-01-15 00:36:02 +0000166 uint32_t Hash = hashStringV1(S);
167
168 for (uint32_t I = 0; I != BucketCount; ++I) {
169 uint32_t Slot = (Hash + I) % BucketCount;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000170 if (Buckets[Slot] != 0)
171 continue;
172 Buckets[Slot] = Offset;
173 break;
174 }
175 }
176
177 if (auto EC = Writer.writeArray(ArrayRef<ulittle32_t>(Buckets)))
178 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000179
180 assert(Writer.bytesRemaining() == 0);
181 return Error::success();
182}
183
184Error PDBStringTableBuilder::writeEpilogue(BinaryStreamWriter &Writer) const {
185 if (auto EC = Writer.writeInteger<uint32_t>(Strings.size()))
Rui Ueyamadcd32932017-01-15 00:36:02 +0000186 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000187 assert(Writer.bytesRemaining() == 0);
188 return Error::success();
189}
190
191Error PDBStringTableBuilder::commit(BinaryStreamWriter &Writer) const {
192 BinaryStreamWriter SectionWriter;
193
194 std::tie(SectionWriter, Writer) = Writer.split(sizeof(PDBStringTableHeader));
195 if (auto EC = writeHeader(SectionWriter))
196 return EC;
197
198 std::tie(SectionWriter, Writer) =
199 Writer.split(Strings.calculateSerializedSize());
200 if (auto EC = writeStrings(SectionWriter))
201 return EC;
202
203 std::tie(SectionWriter, Writer) = Writer.split(calculateHashTableSize());
204 if (auto EC = writeHashTable(SectionWriter))
205 return EC;
206
207 std::tie(SectionWriter, Writer) = Writer.split(sizeof(uint32_t));
208 if (auto EC = writeEpilogue(SectionWriter))
209 return EC;
210
Rui Ueyamadcd32932017-01-15 00:36:02 +0000211 return Error::success();
212}