blob: a472181a4895ce3c5488890a13f43ab59884942a [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"
Zachary Turnerc504ae32017-05-03 15:58:37 +000013#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000014#include "llvm/DebugInfo/PDB/Native/Hash.h"
Zachary Turnerc504ae32017-05-03 15:58:37 +000015#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000016#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000017#include "llvm/Support/BinaryStreamWriter.h"
Rui Ueyamadcd32932017-01-15 00:36:02 +000018#include "llvm/Support/Endian.h"
19
20using 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 Turnere204a6c2017-05-02 18:00:13 +000026uint32_t PDBStringTableBuilder::insert(StringRef S) {
Zachary Turnerc504ae32017-05-03 15:58:37 +000027 return Strings.insert(S);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000028}
29
Rui Ueyamadcd32932017-01-15 00:36:02 +000030static uint32_t computeBucketCount(uint32_t NumStrings) {
31 // The /names stream is basically an on-disk open-addressing hash table.
32 // Hash collisions are resolved by linear probing. We cannot make
33 // utilization 100% because it will make the linear probing extremely
34 // slow. But lower utilization wastes disk space. As a reasonable
35 // load factor, we choose 80%. We need +1 because slot 0 is reserved.
36 return (NumStrings + 1) * 1.25;
37}
38
Zachary Turnerc504ae32017-05-03 15:58:37 +000039uint32_t PDBStringTableBuilder::calculateHashTableSize() const {
40 uint32_t Size = sizeof(uint32_t); // Hash table begins with 4-byte size field.
41 Size += sizeof(uint32_t) * computeBucketCount(Strings.size());
Daniel Jasperdff096f2017-05-03 07:29:25 +000042
Zachary Turner7dba20b2017-05-02 23:36:17 +000043 return Size;
44}
45
Zachary Turnerc504ae32017-05-03 15:58:37 +000046uint32_t PDBStringTableBuilder::calculateSerializedSize() const {
47 uint32_t Size = 0;
48 Size += sizeof(PDBStringTableHeader);
49 Size += Strings.calculateSerializedSize();
50 Size += calculateHashTableSize();
51 Size += sizeof(uint32_t); // The /names stream ends with the string count.
52 return Size;
53}
54
55Error PDBStringTableBuilder::writeHeader(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +000056 // Write a header
Zachary Turnere204a6c2017-05-02 18:00:13 +000057 PDBStringTableHeader H;
58 H.Signature = PDBStringTableSignature;
Rui Ueyamadcd32932017-01-15 00:36:02 +000059 H.HashVersion = 1;
Zachary Turnerc504ae32017-05-03 15:58:37 +000060 H.ByteSize = Strings.calculateSerializedSize();
Rui Ueyamadcd32932017-01-15 00:36:02 +000061 if (auto EC = Writer.writeObject(H))
62 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +000063 assert(Writer.bytesRemaining() == 0);
64 return Error::success();
65}
Rui Ueyamadcd32932017-01-15 00:36:02 +000066
Zachary Turnerc504ae32017-05-03 15:58:37 +000067Error PDBStringTableBuilder::writeStrings(BinaryStreamWriter &Writer) const {
68 if (auto EC = Strings.commit(Writer))
69 return EC;
Rui Ueyamadcd32932017-01-15 00:36:02 +000070
Zachary Turnerc504ae32017-05-03 15:58:37 +000071 assert(Writer.bytesRemaining() == 0);
72 return Error::success();
73}
74
75Error PDBStringTableBuilder::writeHashTable(BinaryStreamWriter &Writer) const {
Rui Ueyamadcd32932017-01-15 00:36:02 +000076 // Write a hash table.
77 uint32_t BucketCount = computeBucketCount(Strings.size());
Zachary Turner695ed562017-02-28 00:04:07 +000078 if (auto EC = Writer.writeInteger(BucketCount))
Rui Ueyamadcd32932017-01-15 00:36:02 +000079 return EC;
80 std::vector<ulittle32_t> Buckets(BucketCount);
81
Zachary Turnerc504ae32017-05-03 15:58:37 +000082 for (auto &Pair : Strings) {
83 StringRef S = Pair.getKey();
84 uint32_t Offset = Pair.getValue();
Rui Ueyamadcd32932017-01-15 00:36:02 +000085 uint32_t Hash = hashStringV1(S);
86
87 for (uint32_t I = 0; I != BucketCount; ++I) {
88 uint32_t Slot = (Hash + I) % BucketCount;
89 if (Slot == 0)
90 continue; // Skip reserved slot
91 if (Buckets[Slot] != 0)
92 continue;
93 Buckets[Slot] = Offset;
94 break;
95 }
96 }
97
98 if (auto EC = Writer.writeArray(ArrayRef<ulittle32_t>(Buckets)))
99 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000100
101 assert(Writer.bytesRemaining() == 0);
102 return Error::success();
103}
104
105Error PDBStringTableBuilder::writeEpilogue(BinaryStreamWriter &Writer) const {
106 if (auto EC = Writer.writeInteger<uint32_t>(Strings.size()))
Rui Ueyamadcd32932017-01-15 00:36:02 +0000107 return EC;
Zachary Turnerc504ae32017-05-03 15:58:37 +0000108 assert(Writer.bytesRemaining() == 0);
109 return Error::success();
110}
111
112Error PDBStringTableBuilder::commit(BinaryStreamWriter &Writer) const {
113 BinaryStreamWriter SectionWriter;
114
115 std::tie(SectionWriter, Writer) = Writer.split(sizeof(PDBStringTableHeader));
116 if (auto EC = writeHeader(SectionWriter))
117 return EC;
118
119 std::tie(SectionWriter, Writer) =
120 Writer.split(Strings.calculateSerializedSize());
121 if (auto EC = writeStrings(SectionWriter))
122 return EC;
123
124 std::tie(SectionWriter, Writer) = Writer.split(calculateHashTableSize());
125 if (auto EC = writeHashTable(SectionWriter))
126 return EC;
127
128 std::tie(SectionWriter, Writer) = Writer.split(sizeof(uint32_t));
129 if (auto EC = writeEpilogue(SectionWriter))
130 return EC;
131
Rui Ueyamadcd32932017-01-15 00:36:02 +0000132 return Error::success();
133}