blob: 3bb4503fe6a31c703a50b51241ee2bf948bec6d8 [file] [log] [blame]
Zachary Turnerfaa554b2016-07-15 22:16:56 +00001//===- NameMapBuilder.cpp - PDB Name Map Builder ----------------*- C++ -*-===//
2//
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
10#include "llvm/DebugInfo/PDB/Raw/NameMapBuilder.h"
11
Zachary Turner334aec42016-07-29 18:38:47 +000012#include "llvm/DebugInfo/Msf/StreamWriter.h"
Zachary Turnerfaa554b2016-07-15 22:16:56 +000013#include "llvm/DebugInfo/PDB/Raw/NameMap.h"
Zachary Turner5e534c72016-07-15 22:17:08 +000014#include "llvm/Support/Endian.h"
Zachary Turnerfaa554b2016-07-15 22:16:56 +000015
16using namespace llvm;
17using namespace llvm::pdb;
18
19NameMapBuilder::NameMapBuilder() {}
20
Zachary Turner5e534c72016-07-15 22:17:08 +000021void NameMapBuilder::addMapping(StringRef Name, uint32_t Mapping) {
22 StringDataBytes += Name.size() + 1;
23 Map.insert({Name, Mapping});
24}
25
Zachary Turnerfaa554b2016-07-15 22:16:56 +000026Expected<std::unique_ptr<NameMap>> NameMapBuilder::build() {
Zachary Turner5e534c72016-07-15 22:17:08 +000027 auto Result = llvm::make_unique<NameMap>();
28 Result->Mapping = Map;
29 return std::move(Result);
Zachary Turnerfaa554b2016-07-15 22:16:56 +000030}
31
32uint32_t NameMapBuilder::calculateSerializedLength() const {
Zachary Turner5e534c72016-07-15 22:17:08 +000033 uint32_t TotalLength = 0;
34
35 TotalLength += sizeof(support::ulittle32_t); // StringDataBytes value
36 TotalLength += StringDataBytes; // actual string data
37
38 TotalLength += sizeof(support::ulittle32_t); // Hash Size
39 TotalLength += sizeof(support::ulittle32_t); // Max Number of Strings
40 TotalLength += sizeof(support::ulittle32_t); // Num Present Words
41 // One bitmask word for each present entry
42 TotalLength += Map.size() * sizeof(support::ulittle32_t);
43 TotalLength += sizeof(support::ulittle32_t); // Num Deleted Words
44
45 // For each present word, which we are treating as equivalent to the number of
46 // entries in the table, we have a pair of integers. An offset into the
47 // string data, and a corresponding stream number.
48 TotalLength += Map.size() * 2 * sizeof(support::ulittle32_t);
49
50 return TotalLength;
Zachary Turnerfaa554b2016-07-15 22:16:56 +000051}
Zachary Turnerd66889c2016-07-28 19:12:28 +000052
53Error NameMapBuilder::commit(msf::StreamWriter &Writer) const {
54 // The first field is the number of bytes of string data. So add
55 // up the length of all strings plus a null terminator for each
56 // one.
57 uint32_t NumBytes = 0;
58 for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
59 NumBytes += B->getKeyLength() + 1;
60 }
61
62 if (auto EC = Writer.writeInteger(NumBytes)) // Number of bytes of string data
63 return EC;
64 // Now all of the string data itself.
65 for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
66 if (auto EC = Writer.writeZeroString(B->getKey()))
67 return EC;
68 }
69
70 if (auto EC = Writer.writeInteger(Map.size())) // Hash Size
71 return EC;
72
73 if (auto EC = Writer.writeInteger(Map.size())) // Max Number of Strings
74 return EC;
75
76 if (auto EC = Writer.writeInteger(Map.size())) // Num Present Words
77 return EC;
78
79 // For each entry in the mapping, write a bit mask which represents a bucket
80 // to store it in. We don't use this, so the value we write isn't important
81 // to us, it just has to be there.
82 for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
83 if (auto EC = Writer.writeInteger(1U))
84 return EC;
85 }
86
87 if (auto EC = Writer.writeInteger(0U)) // Num Deleted Words
88 return EC;
89
90 // Mappings of each word.
91 uint32_t OffsetSoFar = 0;
92 for (auto B = Map.begin(), E = Map.end(); B != E; ++B) {
93 // This is a list of key value pairs where the key is the offset into the
94 // strings buffer, and the value is a stream number. Write each pair.
95 if (auto EC = Writer.writeInteger(OffsetSoFar))
96 return EC;
97
98 if (auto EC = Writer.writeInteger(B->second))
99 return EC;
100
101 OffsetSoFar += B->getKeyLength() + 1;
102 }
103
104 return Error::success();
105}