blob: b2ae1188bb170b944e5f42543fbe93df57904ef3 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rui Ueyamadcd32932017-01-15 00:36:02 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turnere204a6c2017-05-02 18:00:13 +00009#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Zachary Turnerc504ae32017-05-03 15:58:37 +000010
Rui Ueyamadcd32932017-01-15 00:36:02 +000011#include "llvm/ADT/ArrayRef.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000012#include "llvm/DebugInfo/PDB/Native/Hash.h"
13#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000014#include "llvm/Support/BinaryStreamWriter.h"
Rui Ueyamadcd32932017-01-15 00:36:02 +000015#include "llvm/Support/Endian.h"
16
Zachary Turnera6fb5362018-03-23 18:43:39 +000017#include <map>
18
Rui Ueyamadcd32932017-01-15 00:36:02 +000019using namespace llvm;
Zachary Turnerc504ae32017-05-03 15:58:37 +000020using namespace llvm::msf;
Rui Ueyamadcd32932017-01-15 00:36:02 +000021using namespace llvm::support;
22using namespace llvm::support::endian;
23using namespace llvm::pdb;
24
Zachary Turnerf2282762018-03-23 19:57:25 +000025StringTableHashTraits::StringTableHashTraits(PDBStringTableBuilder &Table)
26 : Table(&Table) {}
27
28uint32_t StringTableHashTraits::hashLookupKey(StringRef S) const {
29 return Table->getIdForString(S);
30}
31
32StringRef StringTableHashTraits::storageKeyToLookupKey(uint32_t Offset) const {
33 return Table->getStringForId(Offset);
34}
35
36uint32_t StringTableHashTraits::lookupKeyToStorageKey(StringRef S) {
37 return Table->insert(S);
38}
39
Zachary Turnere204a6c2017-05-02 18:00:13 +000040uint32_t PDBStringTableBuilder::insert(StringRef S) {
Zachary Turnerc504ae32017-05-03 15:58:37 +000041 return Strings.insert(S);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000042}
43
Zachary Turner71d36ad2018-03-22 17:37:28 +000044uint32_t PDBStringTableBuilder::getIdForString(StringRef S) const {
45 return Strings.getIdForString(S);
46}
47
48StringRef PDBStringTableBuilder::getStringForId(uint32_t Id) const {
49 return Strings.getStringForId(Id);
50}
51
Zachary Turnera6fb5362018-03-23 18:43:39 +000052// This is a precomputed list of Buckets given the specified number of
53// strings. Matching the reference algorithm exactly is not strictly
54// necessary for correctness, but it helps when comparing LLD's PDBs with
55// Microsoft's PDBs so as to eliminate superfluous differences.
56static std::map<uint32_t, uint32_t> StringsToBuckets = {
57 {1, 2},
58 {2, 4},
59 {4, 7},
60 {6, 11},
61 {9, 17},
62 {13, 26},
63 {20, 40},
64 {31, 61},
65 {46, 92},
66 {70, 139},
67 {105, 209},
68 {157, 314},
69 {236, 472},
70 {355, 709},
71 {532, 1064},
72 {799, 1597},
73 {1198, 2396},
74 {1798, 3595},
75 {2697, 5393},
76 {4045, 8090},
77 {6068, 12136},
78 {9103, 18205},
79 {13654, 27308},
80 {20482, 40963},
81 {30723, 61445},
82 {46084, 92168},
83 {69127, 138253},
84 {103690, 207380},
85 {155536, 311071},
86 {233304, 466607},
87 {349956, 699911},
88 {524934, 1049867},
89 {787401, 1574801},
90 {1181101, 2362202},
91 {1771652, 3543304},
92 {2657479, 5314957},
93 {3986218, 7972436},
94 {5979328, 11958655},
95 {8968992, 17937983},
96 {13453488, 26906975},
97 {20180232, 40360463},
98 {30270348, 60540695},
99 {45405522, 90811043},
100 {68108283, 136216565},
101 {102162424, 204324848},
102 {153243637, 306487273},
103 {229865455, 459730910},
104 {344798183, 689596366},
105 {517197275, 1034394550},
106 {775795913, 1551591826}};
107
Rui Ueyamadcd32932017-01-15 00:36:02 +0000108static uint32_t computeBucketCount(uint32_t NumStrings) {
Zachary Turnera6fb5362018-03-23 18:43:39 +0000109 auto Entry = StringsToBuckets.lower_bound(NumStrings);
110 assert(Entry != StringsToBuckets.end());
111 return Entry->second;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000112}
113
Zachary Turnerc504ae32017-05-03 15:58:37 +0000114uint32_t PDBStringTableBuilder::calculateHashTableSize() const {
115 uint32_t Size = sizeof(uint32_t); // Hash table begins with 4-byte size field.
116 Size += sizeof(uint32_t) * computeBucketCount(Strings.size());
Daniel Jasperdff096f2017-05-03 07:29:25 +0000117
Zachary Turner7dba20b2017-05-02 23:36:17 +0000118 return Size;
119}
120
Zachary Turnerc504ae32017-05-03 15:58:37 +0000121uint32_t PDBStringTableBuilder::calculateSerializedSize() const {
122 uint32_t Size = 0;
123 Size += sizeof(PDBStringTableHeader);
124 Size += Strings.calculateSerializedSize();
125 Size += calculateHashTableSize();
126 Size += sizeof(uint32_t); // The /names stream ends with the string count.
127 return Size;
128}
129
Zachary Turnera8cfc292017-06-14 15:59:27 +0000130void PDBStringTableBuilder::setStrings(
131 const codeview::DebugStringTableSubsection &Strings) {
132 this->Strings = Strings;
133}
134
Zachary Turnerc504ae32017-05-03 15:58:37 +0000135Error PDBStringTableBuilder::writeHeader(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +0000136 // Write a header
Zachary Turnere204a6c2017-05-02 18:00:13 +0000137 PDBStringTableHeader H;
138 H.Signature = PDBStringTableSignature;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000139 H.HashVersion = 1;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000140 H.ByteSize = Strings.calculateSerializedSize();
Rui Ueyamadcd32932017-01-15 00:36:02 +0000141 if (auto EC = Writer.writeObject(H))
142 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000143 assert(Writer.bytesRemaining() == 0);
144 return Error::success();
145}
Rui Ueyamadcd32932017-01-15 00:36:02 +0000146
Zachary Turnerc504ae32017-05-03 15:58:37 +0000147Error PDBStringTableBuilder::writeStrings(BinaryStreamWriter &Writer) const {
148 if (auto EC = Strings.commit(Writer))
149 return EC;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000150
Zachary Turnerc504ae32017-05-03 15:58:37 +0000151 assert(Writer.bytesRemaining() == 0);
152 return Error::success();
153}
154
155Error PDBStringTableBuilder::writeHashTable(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +0000156 // Write a hash table.
157 uint32_t BucketCount = computeBucketCount(Strings.size());
Zachary Turner695ed562017-02-28 00:04:07 +0000158 if (auto EC = Writer.writeInteger(BucketCount))
Rui Ueyamadcd32932017-01-15 00:36:02 +0000159 return EC;
160 std::vector<ulittle32_t> Buckets(BucketCount);
161
Zachary Turnerc504ae32017-05-03 15:58:37 +0000162 for (auto &Pair : Strings) {
163 StringRef S = Pair.getKey();
164 uint32_t Offset = Pair.getValue();
Rui Ueyamadcd32932017-01-15 00:36:02 +0000165 uint32_t Hash = hashStringV1(S);
166
167 for (uint32_t I = 0; I != BucketCount; ++I) {
168 uint32_t Slot = (Hash + I) % BucketCount;
Rui Ueyamadcd32932017-01-15 00:36:02 +0000169 if (Buckets[Slot] != 0)
170 continue;
171 Buckets[Slot] = Offset;
172 break;
173 }
174 }
175
176 if (auto EC = Writer.writeArray(ArrayRef<ulittle32_t>(Buckets)))
177 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000178
179 assert(Writer.bytesRemaining() == 0);
180 return Error::success();
181}
182
183Error PDBStringTableBuilder::writeEpilogue(BinaryStreamWriter &Writer) const {
184 if (auto EC = Writer.writeInteger<uint32_t>(Strings.size()))
Rui Ueyamadcd32932017-01-15 00:36:02 +0000185 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000186 assert(Writer.bytesRemaining() == 0);
187 return Error::success();
188}
189
190Error PDBStringTableBuilder::commit(BinaryStreamWriter &Writer) const {
191 BinaryStreamWriter SectionWriter;
192
193 std::tie(SectionWriter, Writer) = Writer.split(sizeof(PDBStringTableHeader));
194 if (auto EC = writeHeader(SectionWriter))
195 return EC;
196
197 std::tie(SectionWriter, Writer) =
198 Writer.split(Strings.calculateSerializedSize());
199 if (auto EC = writeStrings(SectionWriter))
200 return EC;
201
202 std::tie(SectionWriter, Writer) = Writer.split(calculateHashTableSize());
203 if (auto EC = writeHashTable(SectionWriter))
204 return EC;
205
206 std::tie(SectionWriter, Writer) = Writer.split(sizeof(uint32_t));
207 if (auto EC = writeEpilogue(SectionWriter))
208 return EC;
209
Rui Ueyamadcd32932017-01-15 00:36:02 +0000210 return Error::success();
211}