1 //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
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/NamedStreamMap.h"
10 #include "llvm/ADT/SparseBitVector.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/DebugInfo/PDB/Native/Hash.h"
14 #include "llvm/DebugInfo/PDB/Native/HashTable.h"
15 #include "llvm/DebugInfo/PDB/Native/RawError.h"
16 #include "llvm/Support/BinaryStreamReader.h"
17 #include "llvm/Support/BinaryStreamWriter.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cstdint>
23
24 using namespace llvm;
25 using namespace llvm::pdb;
26
NamedStreamMapTraits(NamedStreamMap & NS)27 NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {}
28
hashLookupKey(StringRef S) const29 uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const {
30 // In the reference implementation, this uses
31 // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
32 // Here, the type HASH is a typedef of unsigned short.
33 // ** It is not a bug that we truncate the result of hashStringV1, in fact
34 // it is a bug if we do not! **
35 // See NMTNI::hash() in the reference implementation.
36 return static_cast<uint16_t>(hashStringV1(S));
37 }
38
storageKeyToLookupKey(uint32_t Offset) const39 StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const {
40 return NS->getString(Offset);
41 }
42
lookupKeyToStorageKey(StringRef S)43 uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) {
44 return NS->appendStringData(S);
45 }
46
NamedStreamMap()47 NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
48
load(BinaryStreamReader & Stream)49 Error NamedStreamMap::load(BinaryStreamReader &Stream) {
50 uint32_t StringBufferSize;
51 if (auto EC = Stream.readInteger(StringBufferSize))
52 return joinErrors(std::move(EC),
53 make_error<RawError>(raw_error_code::corrupt_file,
54 "Expected string buffer size"));
55
56 StringRef Buffer;
57 if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
58 return EC;
59 NamesBuffer.assign(Buffer.begin(), Buffer.end());
60
61 return OffsetIndexMap.load(Stream);
62 }
63
commit(BinaryStreamWriter & Writer) const64 Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const {
65 // The first field is the number of bytes of string data.
66 if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
67 return EC;
68
69 // Then the actual string data.
70 StringRef Data(NamesBuffer.data(), NamesBuffer.size());
71 if (auto EC = Writer.writeFixedString(Data))
72 return EC;
73
74 // And finally the Offset Index map.
75 if (auto EC = OffsetIndexMap.commit(Writer))
76 return EC;
77
78 return Error::success();
79 }
80
calculateSerializedLength() const81 uint32_t NamedStreamMap::calculateSerializedLength() const {
82 return sizeof(uint32_t) // String data size
83 + NamesBuffer.size() // String data
84 + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
85 }
86
size() const87 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
88
getString(uint32_t Offset) const89 StringRef NamedStreamMap::getString(uint32_t Offset) const {
90 assert(NamesBuffer.size() > Offset);
91 return StringRef(NamesBuffer.data() + Offset);
92 }
93
hashString(uint32_t Offset) const94 uint32_t NamedStreamMap::hashString(uint32_t Offset) const {
95 return hashStringV1(getString(Offset));
96 }
97
get(StringRef Stream,uint32_t & StreamNo) const98 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
99 auto Iter = OffsetIndexMap.find_as(Stream, HashTraits);
100 if (Iter == OffsetIndexMap.end())
101 return false;
102 StreamNo = (*Iter).second;
103 return true;
104 }
105
entries() const106 StringMap<uint32_t> NamedStreamMap::entries() const {
107 StringMap<uint32_t> Result;
108 for (const auto &Entry : OffsetIndexMap) {
109 StringRef Stream(NamesBuffer.data() + Entry.first);
110 Result.try_emplace(Stream, Entry.second);
111 }
112 return Result;
113 }
114
appendStringData(StringRef S)115 uint32_t NamedStreamMap::appendStringData(StringRef S) {
116 uint32_t Offset = NamesBuffer.size();
117 llvm::append_range(NamesBuffer, S);
118 NamesBuffer.push_back('\0');
119 return Offset;
120 }
121
set(StringRef Stream,uint32_t StreamNo)122 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
123 OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
124 }
125