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