blob: ece3e00b1a87edc9bf12bd4b0df74b3a0524a464 [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
18using namespace llvm;
Zachary Turnerc504ae32017-05-03 15:58:37 +000019using namespace llvm::msf;
Rui Ueyamadcd32932017-01-15 00:36:02 +000020using namespace llvm::support;
21using namespace llvm::support::endian;
22using namespace llvm::pdb;
23
Zachary Turnere204a6c2017-05-02 18:00:13 +000024uint32_t PDBStringTableBuilder::insert(StringRef S) {
Zachary Turnerc504ae32017-05-03 15:58:37 +000025 return Strings.insert(S);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000026}
27
Rui Ueyamadcd32932017-01-15 00:36:02 +000028static uint32_t computeBucketCount(uint32_t NumStrings) {
29 // The /names stream is basically an on-disk open-addressing hash table.
30 // Hash collisions are resolved by linear probing. We cannot make
31 // utilization 100% because it will make the linear probing extremely
32 // slow. But lower utilization wastes disk space. As a reasonable
33 // load factor, we choose 80%. We need +1 because slot 0 is reserved.
34 return (NumStrings + 1) * 1.25;
35}
36
Zachary Turnerc504ae32017-05-03 15:58:37 +000037uint32_t PDBStringTableBuilder::calculateHashTableSize() const {
38 uint32_t Size = sizeof(uint32_t); // Hash table begins with 4-byte size field.
39 Size += sizeof(uint32_t) * computeBucketCount(Strings.size());
Daniel Jasperdff096f2017-05-03 07:29:25 +000040
Zachary Turner7dba20b2017-05-02 23:36:17 +000041 return Size;
42}
43
Zachary Turnerc504ae32017-05-03 15:58:37 +000044uint32_t PDBStringTableBuilder::calculateSerializedSize() const {
45 uint32_t Size = 0;
46 Size += sizeof(PDBStringTableHeader);
47 Size += Strings.calculateSerializedSize();
48 Size += calculateHashTableSize();
49 Size += sizeof(uint32_t); // The /names stream ends with the string count.
50 return Size;
51}
52
Zachary Turnera8cfc292017-06-14 15:59:27 +000053void PDBStringTableBuilder::setStrings(
54 const codeview::DebugStringTableSubsection &Strings) {
55 this->Strings = Strings;
56}
57
Zachary Turnerc504ae32017-05-03 15:58:37 +000058Error PDBStringTableBuilder::writeHeader(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +000059 // Write a header
Zachary Turnere204a6c2017-05-02 18:00:13 +000060 PDBStringTableHeader H;
61 H.Signature = PDBStringTableSignature;
Rui Ueyamadcd32932017-01-15 00:36:02 +000062 H.HashVersion = 1;
Zachary Turnerc504ae32017-05-03 15:58:37 +000063 H.ByteSize = Strings.calculateSerializedSize();
Rui Ueyamadcd32932017-01-15 00:36:02 +000064 if (auto EC = Writer.writeObject(H))
65 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +000066 assert(Writer.bytesRemaining() == 0);
67 return Error::success();
68}
Rui Ueyamadcd32932017-01-15 00:36:02 +000069
Zachary Turnerc504ae32017-05-03 15:58:37 +000070Error PDBStringTableBuilder::writeStrings(BinaryStreamWriter &Writer) const {
71 if (auto EC = Strings.commit(Writer))
72 return EC;
Rui Ueyamadcd32932017-01-15 00:36:02 +000073
Zachary Turnerc504ae32017-05-03 15:58:37 +000074 assert(Writer.bytesRemaining() == 0);
75 return Error::success();
76}
77
78Error PDBStringTableBuilder::writeHashTable(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +000079 // Write a hash table.
80 uint32_t BucketCount = computeBucketCount(Strings.size());
Zachary Turner695ed562017-02-28 00:04:07 +000081 if (auto EC = Writer.writeInteger(BucketCount))
Rui Ueyamadcd32932017-01-15 00:36:02 +000082 return EC;
83 std::vector<ulittle32_t> Buckets(BucketCount);
84
Zachary Turnerc504ae32017-05-03 15:58:37 +000085 for (auto &Pair : Strings) {
86 StringRef S = Pair.getKey();
87 uint32_t Offset = Pair.getValue();
Rui Ueyamadcd32932017-01-15 00:36:02 +000088 uint32_t Hash = hashStringV1(S);
89
90 for (uint32_t I = 0; I != BucketCount; ++I) {
91 uint32_t Slot = (Hash + I) % BucketCount;
92 if (Slot == 0)
93 continue; // Skip reserved slot
94 if (Buckets[Slot] != 0)
95 continue;
96 Buckets[Slot] = Offset;
97 break;
98 }
99 }
100
101 if (auto EC = Writer.writeArray(ArrayRef<ulittle32_t>(Buckets)))
102 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000103
104 assert(Writer.bytesRemaining() == 0);
105 return Error::success();
106}
107
108Error PDBStringTableBuilder::writeEpilogue(BinaryStreamWriter &Writer) const {
109 if (auto EC = Writer.writeInteger<uint32_t>(Strings.size()))
Rui Ueyamadcd32932017-01-15 00:36:02 +0000110 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000111 assert(Writer.bytesRemaining() == 0);
112 return Error::success();
113}
114
115Error PDBStringTableBuilder::commit(BinaryStreamWriter &Writer) const {
116 BinaryStreamWriter SectionWriter;
117
118 std::tie(SectionWriter, Writer) = Writer.split(sizeof(PDBStringTableHeader));
119 if (auto EC = writeHeader(SectionWriter))
120 return EC;
121
122 std::tie(SectionWriter, Writer) =
123 Writer.split(Strings.calculateSerializedSize());
124 if (auto EC = writeStrings(SectionWriter))
125 return EC;
126
127 std::tie(SectionWriter, Writer) = Writer.split(calculateHashTableSize());
128 if (auto EC = writeHashTable(SectionWriter))
129 return EC;
130
131 std::tie(SectionWriter, Writer) = Writer.split(sizeof(uint32_t));
132 if (auto EC = writeEpilogue(SectionWriter))
133 return EC;
134
Rui Ueyamadcd32932017-01-15 00:36:02 +0000135 return Error::success();
136}