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 22 using namespace llvm; 23 using namespace llvm::pdb; 24 25 NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {} 26 27 uint16_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(S)); 35 } 36 37 StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const { 38 return NS->getString(Offset); 39 } 40 41 uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) { 42 return NS->appendStringData(S); 43 } 44 45 NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {} 46 47 Error NamedStreamMap::load(BinaryStreamReader &Stream) { 48 uint32_t StringBufferSize; 49 if (auto EC = Stream.readInteger(StringBufferSize)) 50 return joinErrors(std::move(EC), 51 make_error<RawError>(raw_error_code::corrupt_file, 52 "Expected string buffer size")); 53 54 StringRef Buffer; 55 if (auto EC = Stream.readFixedString(Buffer, StringBufferSize)) 56 return EC; 57 NamesBuffer.assign(Buffer.begin(), Buffer.end()); 58 59 return OffsetIndexMap.load(Stream); 60 } 61 62 Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const { 63 // The first field is the number of bytes of string data. 64 if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size())) 65 return EC; 66 67 // Then the actual string data. 68 StringRef Data(NamesBuffer.data(), NamesBuffer.size()); 69 if (auto EC = Writer.writeFixedString(Data)) 70 return EC; 71 72 // And finally the Offset Index map. 73 if (auto EC = OffsetIndexMap.commit(Writer)) 74 return EC; 75 76 return Error::success(); 77 } 78 79 uint32_t NamedStreamMap::calculateSerializedLength() const { 80 return sizeof(uint32_t) // String data size 81 + NamesBuffer.size() // String data 82 + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map 83 } 84 85 uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); } 86 87 StringRef NamedStreamMap::getString(uint32_t Offset) const { 88 assert(NamesBuffer.size() > Offset); 89 return StringRef(NamesBuffer.data() + Offset); 90 } 91 92 uint32_t NamedStreamMap::hashString(uint32_t Offset) const { 93 return hashStringV1(getString(Offset)); 94 } 95 96 bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const { 97 auto Iter = OffsetIndexMap.find_as(Stream, HashTraits); 98 if (Iter == OffsetIndexMap.end()) 99 return false; 100 StreamNo = (*Iter).second; 101 return true; 102 } 103 104 StringMap<uint32_t> NamedStreamMap::entries() const { 105 StringMap<uint32_t> Result; 106 for (const auto &Entry : OffsetIndexMap) { 107 StringRef Stream(NamesBuffer.data() + Entry.first); 108 Result.try_emplace(Stream, Entry.second); 109 } 110 return Result; 111 } 112 113 uint32_t NamedStreamMap::appendStringData(StringRef S) { 114 uint32_t Offset = NamesBuffer.size(); 115 llvm::append_range(NamesBuffer, S); 116 NamesBuffer.push_back('\0'); 117 return Offset; 118 } 119 120 void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) { 121 OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits); 122 } 123