1 //===- PDBStringTableBuilder.cpp - PDB String Table -------------*- C++ -*-===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" 10 11 #include "llvm/ADT/ArrayRef.h" 12 #include "llvm/DebugInfo/PDB/Native/Hash.h" 13 #include "llvm/DebugInfo/PDB/Native/RawTypes.h" 14 #include "llvm/Support/BinaryStreamWriter.h" 15 #include "llvm/Support/Endian.h" 16 17 #include <map> 18 19 using namespace llvm; 20 using namespace llvm::msf; 21 using namespace llvm::support; 22 using namespace llvm::support::endian; 23 using namespace llvm::pdb; 24 25 StringTableHashTraits::StringTableHashTraits(PDBStringTableBuilder &Table) 26 : Table(&Table) {} 27 28 uint32_t StringTableHashTraits::hashLookupKey(StringRef S) const { 29 return Table->getIdForString(S); 30 } 31 32 StringRef StringTableHashTraits::storageKeyToLookupKey(uint32_t Offset) const { 33 return Table->getStringForId(Offset); 34 } 35 36 uint32_t StringTableHashTraits::lookupKeyToStorageKey(StringRef S) { 37 return Table->insert(S); 38 } 39 40 uint32_t PDBStringTableBuilder::insert(StringRef S) { 41 return Strings.insert(S); 42 } 43 44 uint32_t PDBStringTableBuilder::getIdForString(StringRef S) const { 45 return Strings.getIdForString(S); 46 } 47 48 StringRef PDBStringTableBuilder::getStringForId(uint32_t Id) const { 49 return Strings.getStringForId(Id); 50 } 51 52 static uint32_t computeBucketCount(uint32_t NumStrings) { 53 // 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. 57 static 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 auto Entry = StringsToBuckets.lower_bound(NumStrings); 109 assert(Entry != StringsToBuckets.end()); 110 return Entry->second; 111 } 112 113 uint32_t PDBStringTableBuilder::calculateHashTableSize() const { 114 uint32_t Size = sizeof(uint32_t); // Hash table begins with 4-byte size field. 115 Size += sizeof(uint32_t) * computeBucketCount(Strings.size()); 116 117 return Size; 118 } 119 120 uint32_t PDBStringTableBuilder::calculateSerializedSize() const { 121 uint32_t Size = 0; 122 Size += sizeof(PDBStringTableHeader); 123 Size += Strings.calculateSerializedSize(); 124 Size += calculateHashTableSize(); 125 Size += sizeof(uint32_t); // The /names stream ends with the string count. 126 return Size; 127 } 128 129 void PDBStringTableBuilder::setStrings( 130 const codeview::DebugStringTableSubsection &Strings) { 131 this->Strings = Strings; 132 } 133 134 Error PDBStringTableBuilder::writeHeader(BinaryStreamWriter &Writer) const { 135 // Write a header 136 PDBStringTableHeader H; 137 H.Signature = PDBStringTableSignature; 138 H.HashVersion = 1; 139 H.ByteSize = Strings.calculateSerializedSize(); 140 if (auto EC = Writer.writeObject(H)) 141 return EC; 142 assert(Writer.bytesRemaining() == 0); 143 return Error::success(); 144 } 145 146 Error PDBStringTableBuilder::writeStrings(BinaryStreamWriter &Writer) const { 147 if (auto EC = Strings.commit(Writer)) 148 return EC; 149 150 assert(Writer.bytesRemaining() == 0); 151 return Error::success(); 152 } 153 154 Error PDBStringTableBuilder::writeHashTable(BinaryStreamWriter &Writer) const { 155 // Write a hash table. 156 uint32_t BucketCount = computeBucketCount(Strings.size()); 157 if (auto EC = Writer.writeInteger(BucketCount)) 158 return EC; 159 std::vector<ulittle32_t> Buckets(BucketCount); 160 161 for (auto &Pair : Strings) { 162 StringRef S = Pair.getKey(); 163 uint32_t Offset = Pair.getValue(); 164 uint32_t Hash = hashStringV1(S); 165 166 for (uint32_t I = 0; I != BucketCount; ++I) { 167 uint32_t Slot = (Hash + I) % BucketCount; 168 if (Buckets[Slot] != 0) 169 continue; 170 Buckets[Slot] = Offset; 171 break; 172 } 173 } 174 175 if (auto EC = Writer.writeArray(ArrayRef<ulittle32_t>(Buckets))) 176 return EC; 177 178 assert(Writer.bytesRemaining() == 0); 179 return Error::success(); 180 } 181 182 Error PDBStringTableBuilder::writeEpilogue(BinaryStreamWriter &Writer) const { 183 if (auto EC = Writer.writeInteger<uint32_t>(Strings.size())) 184 return EC; 185 assert(Writer.bytesRemaining() == 0); 186 return Error::success(); 187 } 188 189 Error PDBStringTableBuilder::commit(BinaryStreamWriter &Writer) const { 190 BinaryStreamWriter SectionWriter; 191 192 std::tie(SectionWriter, Writer) = Writer.split(sizeof(PDBStringTableHeader)); 193 if (auto EC = writeHeader(SectionWriter)) 194 return EC; 195 196 std::tie(SectionWriter, Writer) = 197 Writer.split(Strings.calculateSerializedSize()); 198 if (auto EC = writeStrings(SectionWriter)) 199 return EC; 200 201 std::tie(SectionWriter, Writer) = Writer.split(calculateHashTableSize()); 202 if (auto EC = writeHashTable(SectionWriter)) 203 return EC; 204 205 std::tie(SectionWriter, Writer) = Writer.split(sizeof(uint32_t)); 206 if (auto EC = writeEpilogue(SectionWriter)) 207 return EC; 208 209 return Error::success(); 210 } 211