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